sum

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5776

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


##题意:

判断给定的数串中是否存在连续子串的和能被m整除.


##题解:

维护每个前缀和的余数即可. 如果有两个前缀和的余数相同,那么这两段之差构成的字串一定能被m整除.
WA了一发:cnt[0]要初始化为1.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 201000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n,m;

int cnt[5100];

int main(void)

{

//IN;

int t; cin >> t;
while(t--)
{
memset(cnt, 0, sizeof(cnt));
cin >> n >> m; int sum = 0;
cnt[0]++;
for(int i=1; i<=n; i++) {
int x; scanf("%d", &x);
sum = (sum + x) % m;
cnt[sum]++;
} bool flag = 0;
for(int i=0; i<m; i++) if(cnt[i] > 1) {
flag = 1; break;
} if(flag) puts("YES");
else puts("NO");
} return 0;

}

HDU 5776 sum (模拟)的更多相关文章

  1. HDU 5776 sum(抽屉原理)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=5776 Problem Description Given a sequence, you're ask ...

  2. HDU 5776 sum (思维题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 题目让你求是否有区间的和是m的倍数. 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续 ...

  3. HDU 5776 sum (前缀和)

    题意:给定 n 个数,和 m,问你是不是存在连续的数和是m的倍数. 析:考虑前缀和,如果有两个前缀和取模m相等,那么就是相等的,一定要注意,如果取模为0,就是真的,不要忘记了,我当时就没记得.... ...

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

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

  5. HDU 5776 sum

    猜了一下,发现对了.n>m是一定有解的.所以最多m*m暴力,一定能找到.而T较小,所以能过. #pragma comment(linker, "/STACK:1024000000,10 ...

  6. hdu 5776 sum 前缀和

    sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

  7. HDU 5776 sum( 鸽巢定理简单题 )

    链接:传送门 题意:给一个长为 n 的串,问是否有子串的和是 m 的倍数. 思路:典型鸽巢定理的应用,但是这里 n,m 的大小关系是不确定的,如果 n >= m 根据定理可以很简单的判定是一定有 ...

  8. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  9. HDU 4432 Sum of divisors (进制模拟)

    三个小函数 getdiv();        求因子 getsum();     求平方和 change();     转换成该进制 #include <cstdio> #include ...

随机推荐

  1. Objective-C命名编写规范

    There are only two hard things in Computer Science: cache invalidation and naming things. 在计算机科学中只有两 ...

  2. PHPnow 升级后 PHP不支持GD、MySQL

    来自http://tunps.com/php-unsupport-gd-and-mysql-after-upgrade-phpnow 最近磁盘格式化误操作后,最近两天都在忙于数据恢复,现在才恢复正常. ...

  3. 【C#学习笔记】播放wav文件

    using System; using System.Media; namespace ConsoleApplication { class Program { static void Main(st ...

  4. Swift compile slow 编译慢问题

    http://stackoverflow.com/questions/29707622/bizarre-swift-compiler-error-expression-too-complex-on-a ...

  5. Delphi EVariantTypeCastError错误的解决方法

    在执行程序的时候总是提示: ---------------------------Debugger Exception Notification---------------------------P ...

  6. MySQL基础之第8章 视图

    8.1.视图简介 视图由数据库中的一个表,视图或多个表,视图导出的虚拟表.其作用是方便用户对数据的操作. 8.2.创建视图必须要有CREATE VIEW 和 SELECT 权限SELECT selec ...

  7. SharePoint 2010遍历文档库中所有的文件,文件夹

    转:http://hi.baidu.com/sygwin/item/f99600849d51a12b110ef3eb 创建一个可视WebPart,并拖放一个label控件到ascx文件上,用于显示结果 ...

  8. 记录一次MySQL复制问题的处理

    备库: mysql> show slave status\G*************************** 1. row *************************** Slav ...

  9. Oracle 数据乱码

    原文 Oracle 数据乱码 服务器配置环境变量 NLS_LANG:American_america.ZHS16GBK        

  10. logback.xml_appender配置

    logback<appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的组件. < ...