首页 > 技术文章 > Hdu 5776 sum

whistle13326 原文

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 sum is divisible by m. output YES, otherwise output NO
 
Input
The first line of the input has an integer T (1T10 ), which represents the number of test cases.
For each test case, there are two lines:
1.The first line contains two positive integers n, m (1n100000, 1m5000 ).
2.The second line contains n positive integers x (1x100) according to the sequence.
 
Output
Output T lines, each line print a YES or NO.
 
Sample Input
2 3 3 1 2 3 5 7 6 6 6 6 6
 
Sample Output
YES NO
 
Source
 
询问是否有一段区间和%m为 0
 
 1 /*
 2     前缀和取模 
 3     对于前缀和sum[1-i]%m==k
 4               sum[i-j]%m==k
 5     一定有sum[i+1-j]%m==0 
 6 */
 7 #include <cctype>
 8 #include <cstdio>
 9 #include <cstring>
10 
11 const int MAXN=100010;
12 
13 int T,n,m;
14 
15 int sum[MAXN],cur[MAXN];
16 
17 inline void read(int&x) {
18     int f=1;register char c=getchar();
19     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
20     for(;isdigit(c);x=x*10+c-48,c=getchar());
21     x=x*f;
22 }
23 
24 int hh() {
25     read(T);
26     while(T--) {
27         read(n);read(m);
28         bool flag=false;
29         sum[0]=0;
30         memset(cur,0,sizeof cur);
31         for(int i=1;i<=n;++i) {
32             read(sum[i]);
33               sum[i]=(sum[i]+sum[i-1])%m;
34               ++cur[sum[i]];
35               if(cur[sum[i]]>1||sum[i]==0) flag=true;
36         }
37         if(flag) printf("YES
");
38         else printf("NO
");
39     }
40     return 0;
41 }
42 
43 int sb=hh();
44 int main(int argc,char**argv) {;}
代码

推荐阅读