sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1637 Accepted Submission(s): 647
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 (
1≤T≤10
), 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 ( 1≤n≤100000 , 1≤m≤5000 ).
2.The second line contains n positive integers x ( 1≤x≤100 ) according to the sequence.
For each test case, there are two lines:
1.The first line contains two positive integers n, m ( 1≤n≤100000 , 1≤m≤5000 ).
2.The second line contains n positive integers x ( 1≤x≤100 ) 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存在能整除m的子序列,在子序列的前面的数字和对m取余的值一定等于子序列 后面数字和对m取余的值,需要注意的特殊情况 当刚开始的一个序列就是m的倍数,而后面的 序列和并不是m的倍数,这就是一个bug,只需要让vis[0]=1即可#include<cstdio> #define maxn 200000 int vis[maxn]; int a[maxn],sa[maxn]; int main() { int t; scanf("%d",&t); while(t--) { int i,n,m,flag=0; scanf("%d%d",&n,&m); for(i=1;i<m;++i) vis[i]=0; vis[0]=1; sa[0]=0; for(i=1;i<=n;++i) { scanf("%d",&a[i]); a[i]%=m; sa[i]=(a[i]+sa[i-1])%m; if(vis[sa[i]]) flag=1; vis[sa[i]]=1; } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }