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

HDU 5776 sum (思维题)

时间:2016-07-31 00:04:18      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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 (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 (1n1000001m5000).
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
 

HDU 5776 sum (思维题)

标签:

原文地址:http://www.cnblogs.com/Recoder/p/5722097.html

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