sum

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 2547 Accepted Submission(s): 973

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

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int maxn=100000+10;
int a[maxn];
int b[5010];
int main() {
int t;
cin>>t;
while(t--) {
memset(b,0,sizeof(b));
int n,m;
cin>>n>>m;
cin>>a[0];
a[0]=a[0]%m;
b[b[0]]++;
int flag=0;
for(int i=1;i<n;i++) {
cin>>a[i];
a[i]=(a[i]+a[i-1])%m;
b[a[i]]++;
if(a[i]==0||b[a[i]]>=2) {
flag=1;
}
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

题解:首先已知:若 x % m = b 且 y % m = b,那么x可以写成x = a1 * m + b,y可以写成y = a2 * m + b,(y - x) % m = ((a2 - a1) * m) % m = 0

当模m后,余数在【0,m)之间,相当于共有m个抽屉,序号从0~m-1,放入m+1个物体,其中必定有两个物体在同一个抽屉之中,利用上述定理得,该序列能整除m

hdu 5776 抽屉定理的更多相关文章

  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( 鸽巢定理简单题 )

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

  3. HDU 5776 sum (模拟)

    sum 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5776 Description Given a sequence, you're asked ...

  4. HDU 5776 sum (思维题)

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

  5. HDU 5776

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

  6. HDU 3037(Lucas定理)

    对于很大的组合数不能用C(n, m) = C(n - 1, m) + C(n-1, m -1)来求,这里就用到Lucas定理. 模板题: hdu3037:模板如下: #include <cstd ...

  7. 【抽屉定理】 组合数学poj2356

    假定n个数为a1,a2,...,an,前n项和分别是S1.S2.....Sn,那么如果有一个Si模n是0,就是答案,否则,n个数模n的余数只能在 1到n - 1之间,把余数作为抽屉,显然n个数放到n ...

  8. hdu 3547 (polya定理 + 小高精)

    DIY CubeTime Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  9. HDU 4671 Partition(定理题)

    题目链接 这题,明显考察搜索能力...在中文版的维基百科中找到了公式. #include <cstdio> #include <cstring> #include <st ...

随机推荐

  1. Win32汇编学习(11):对话框(2)

    我们将进一步学习对话框,探讨如何把对话框当成输入设备.如果您看了前一篇文章,那就会发现这次的例子只有少量的改动,就是把我们的对话框窗口附属到主窗口上.另外,我们还要学习通用对话框的用法. 理论: 把对 ...

  2. ActiveReports 大数据分析报告:2018中国电影再次迎来黄金时代

    回顾2018,中国电影市场收获颇丰.先是凭借春节档<红海行动>.<唐人街探案>双双实现30亿票房突破,而后暑期档火力全开,<我不是药神>.<西虹市首富> ...

  3. lib下的Jar包在项目打包的时候提示不能找不到

    maven 使用本地包 lib jar包 依赖一个lib目录 解决方法: <plugin> <groupId>org.apache.maven.plugins</grou ...

  4. Java GC机制

    GC机制的基本算法是:分代收集,这个不用赘述.下面阐述每个分代的收集方法. 年轻代: 事实上,在上一节,已经介绍了新生代的主要垃圾回收方法,在新生代中,使用“停止-复制”算法进行清理,将新生代内存分为 ...

  5. MySQL连接java

    1.创建一个java project,建一个lib文件. 下载MySQL-connecor. 将下载好的文件打开,将mysql-connector-java-5.0.8-bin.jar文件粘贴复制到l ...

  6. 清理SuperMap三维缓存

    一.iclient三维插件浏览器端缓存 方法1:遍历users子文件夹,发现指定目录有supermap,即删除 @echo offtitle 清理三维缓存@echo 开始清理三维缓存...for /f ...

  7. mysql 常用sql语句

    权限 撤销权限revoke all on *.* from 'root'@'192.168.0.197' ; 撤销权限revoke all on *.* from 'xx_db' @'%'; 给指定用 ...

  8. mongodump and mongorestore

    mongoexport和mongoimport只能导出/导入某个特定集合 1 mongoexport bin目录下 ./mongoexport <hostname><:port> ...

  9. 『TensorFlow』读书笔记_ResNet_V2

    『PyTorch × TensorFlow』第十七弹_ResNet快速实现 要点 神经网络逐层加深有Degradiation问题,准确率先上升到饱和,再加深会下降,这不是过拟合,是测试集和训练集同时下 ...

  10. koa和express对比

    不同: 1.启动方式不同 koa采用了new Koa()的方式,而express采用传统的函数形式 2.中间件形式二者不一样,这是由二者处理中间件的逻辑差异导致的,实际上这也是二者最根本的差别 3.k ...