Hdu 5776 sum

Posted 心之所向 素履以往

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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\n");
38         else printf("NO\n");
39     }
40     return 0;
41 }
42 
43 int sb=hh();
44 int main(int argc,char**argv) {;}
代码

 

以上是关于Hdu 5776 sum的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5776 Sum

HDU - 5776 同余(思维)

HDU - 5776 同余(思维)

1 代码片段1

题解报告:hdu 4704 Sum

LeetCode5776. 判断矩阵经轮转后是否一致(C++)