HDU 5776 sum (思维题)

题目链接: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

BestCoder Round #85

时间: 2024-10-23 00:07:48

HDU 5776 sum (思维题)的相关文章

HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <map> #include <queue> #include <vect

hdu 4550 贪心 思维题 不错

http://acm.hdu.edu.cn/showproblem.php?pid=4550 想了挺久,然后各种分类 终于AC,如果是现场,对自己没信心的话,估计还是要WA,,,,,,然后搜题解,发现人家都认为是简单题,看来我还是太弱了,牡丹江没有做出来K看来还是自己贪心和思维有问题 d是一个Deque 最朴素的算法是,如果当前的数<=d.front(),那么插入队列的前面,否则插入队列后面,但是有零所以需要单独处理,还是自己多举例找规律 我的策略: 1.记录0的个数zero,最小非零的数的个数

Hdu 5776 sum

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

hdu 4803 贪心/思维题

http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上键,最后下键补全,可是例子都过不了..... 题解參考http://www.cnblogs.com/xuesu/p/3967704.html http://www.cnblogs.com/Canon-CSU/p/3451784.html http://blog.csdn.net/keshuai199

HDU 5776 sum (前缀和)

题意:给定 n 个数,和 m,问你是不是存在连续的数和是m的倍数. 析:考虑前缀和,如果有两个前缀和取模m相等,那么就是相等的,一定要注意,如果取模为0,就是真的,不要忘记了,我当时就没记得.... 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #i

hdu 4474 BFS+思维题

http://acm.hdu.edu.cn/showproblem.php?pid=4474 如果A%n ==B %n  (A<B) 那么A接下来A经若干次填位数使得A'%n==0,这个过程也可以使B'%n==0  但是显然A更小,所以开一个1e4的数组判重 犯得二逼错误: 1.需要记录每一位,不是mod%10就是每一位 2.第一位枚举1~9,但是仍然需要%n 3.必然需要高精度,开始ll  WA到死 #include <cstdio> #include <cstring>

HDU 6047 贪心思维题

Maximum Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1797    Accepted Submission(s): 842 Problem Description Steph is extremely obsessed with "sequence problems" that are usuall

hdu 4883 思维题

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 566    Accepted Submission(s): 267 Problem Description TIANKENG manages a

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &