题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=5776
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
直接拿这道代码改了一下,两道题一个意思http://www.cnblogs.com/Annetree/p/7095096.html
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<string.h> 6 #include<cmath> 7 using namespace std; 8 #define N 100005 9 10 11 struct node 12 { 13 int num,r; 14 }k[N]; 15 16 bool cmp(node a,node b) 17 { 18 if(a.r==b.r) 19 return a.num<b.num; 20 return a.r<b.r; 21 } 22 23 int main() 24 { 25 long long sum,a; 26 int n,m; 27 bool flag; 28 int T; 29 scanf("%d",&T); 30 while(T--) 31 { 32 scanf("%d%d",&n,&m); 33 flag=false; 34 if(n>m) 35 flag=true; 36 sum=0; 37 k[0].num=0; 38 for(int i=1;i<=n;i++) 39 { 40 scanf("%lld",&a); 41 sum+=a; 42 k[i].r=sum%m; 43 k[i].num=i; 44 if(k[i].r==0&&flag==false) 45 { 46 flag=true; 47 } 48 } 49 if(!flag) 50 { 51 sort(k+1,k+1+n,cmp); 52 for(int i=2;i<=n;i++) 53 { 54 if(k[i].r==k[i-1].r) 55 { 56 flag=true; 57 break; 58 } 59 } 60 } 61 if(!flag) 62 printf("NO "); 63 else 64 printf("YES "); 65 } 66 return 0; 67 }