码迷,mamicode.com
首页 > 其他好文 > 详细

Hdu 5776 sum

时间:2017-09-13 22:09:15      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:nbsp   pen   set   hid   i+1   include   blog   exist   chmod   

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

标签:nbsp   pen   set   hid   i+1   include   blog   exist   chmod   

原文地址:http://www.cnblogs.com/whistle13326/p/7517839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!