HDU 5776 sum (前缀和)

题意:给定 n 个数,和 m,问你是不是存在连续的数和是m的倍数。

析:考虑前缀和,如果有两个前缀和取模m相等,那么就是相等的,一定要注意,如果取模为0,就是真的,不要忘记了,我当时就没记得。。。。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int vis[maxn];

int main(){
   // ios::sync_with_stdio(false);
    int T, q;  cin >> T;
    while(T--){
        scanf("%d %d", &n, &m);
        LL sum = 0;
        memset(vis, 0, sizeof(vis));
        bool ok = false;
        vis[0] = 1;
        for(int i = 0; i < n; ++i){
            scanf("%d", &q);
            sum += q;
            if(vis[sum%m])  ok = true;
            vis[sum%m] = 1;
        }
        printf("%s\n", ok ? "YES" : "NO");
    }
    return 0;
}
时间: 2024-08-02 23:10:34

HDU 5776 sum (前缀和)的相关文章

HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <map> #include <queue> #include <vect

HDU 5776 sum (思维题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 题目让你求是否有区间的和是m的倍数. 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续子列可以组成m的倍数. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cs

Hdu 5776 sum

sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3295    Accepted Submission(s): 1210 Problem Description Given a sequence, you're asked whether there exists a consecutive subsequence whose

hdu 1258 Sum It Up (dfs+路径记录)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3953    Accepted Submission(s): 2032 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

HDU 4704 Sum( 费马小定理 )

HDU 4704 Sum( 费马小定理 ) 理解能力果然拙计,,题目看半天没懂什么意思. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MOD 1000000007 char str[100010]; LL fast_mod( LL a, int b) { LL res = 1; while( b )

hdu 4704 Sum (费马小定理+快速幂)

//(2^n-1)%mod //费马小定理:a^n ≡ a^(n%(m-1)) * a^(m-1)≡ a^(n%(m-1)) (mod m) # include <stdio.h> # include <algorithm> # include <string.h> # define mod 1000000007 using namespace std; __int64 pow(__int64 n) { __int64 p=1,q=2; while(n) { if(n%

HDU 1024Max Sum Plus Plus(最大m字段和)

/* 动态转移方程:dp[i][j]=max(dp[i-1]+a[i], max(dp[t][j-1])+a[i]) (j-1<=t<i) 表示的是前i个数j个字段和的最大值是多少! */ 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define N 10000 5 using namespace std; 6 7 int dp[N][N], num[N]; 8 9 int m

HDU 5776

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 求是否有区间的和是m的倍数 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续子列可以组成m的倍数. 证明:若 x % m = b 且 y % m = b,那么x可以写成x = a1 * m + b,y可以写成y = a2 * m + b,(y - x) % m = ((a2 - a1) * m) % m = 0  假设1-n个数  sum[1-i]%m=sum[1-j]%m  所以s

补写:Best Coder #85 1001 Sum(前缀和)

sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Description Given a sequence, you're asked whether there exists a consecutive subsequence whose sum is divisible by m. outpu