题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776
题目让你求是否有区间的和是m的倍数。
预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续子列可以组成m的倍数。
1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio> 7 #include <vector> 8 #include <cmath> 9 #include <ctime> 10 #include <list> 11 #include <set> 12 #include <map> 13 using namespace std; 14 typedef long long LL; 15 typedef pair <int, int> P; 16 const int N = 1e5 + 5; 17 int a[N], cnt[N]; 18 19 int main() 20 { 21 int t, n, k; 22 scanf("%d", &t); 23 while(t--) { 24 scanf("%d %d", &n, &k); 25 memset(cnt, 0, sizeof(cnt)); 26 bool ok = false; 27 for(int i = 1 ; i <= n ; ++i) { 28 scanf("%d", a + i); 29 a[i] = (a[i] + a[i - 1]) % k; 30 cnt[a[i]]++; 31 if(cnt[a[i]] > 1 || !a[i]) 32 ok = true; 33 } 34 if(ok) 35 printf("YES\n"); 36 else 37 printf("NO\n"); 38 } 39 return 0; 40 }
sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 264 Accepted Submission(s): 127
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.
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