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. Docker入门 配置篇

    docker配置 http://www.runoob.com/docker/docker-tutorial.html

  2. Configuration in ASP.NET Core(未完,待续)

    Configuration in ASP.NET Core App configuration in ASP.NET Core is based on key-value pairs establis ...

  3. Yarn 踩坑 : ERROR: Cannot find configuration directory "/xxxx/xxxx/xxxxx/hadoop-x.x.x/conf"

    解决:在 yarn-env.sh 中,指定 YARN_CONF_DIR 为 hadoop 目录的 /etc/hadoop 在yarn-env.sh 中,配置: export YARN_CONF_DIR ...

  4. scp免密操作

    scp免密操作 2.1服务器(本机)从目标服务器上传/下载文件或者文件夹 2.2生成秘钥 本机执行:ssh-keygen -t rsa 遇到提示,直接回车就OK,秘钥生成在用户的根目录的.ssh目录下 ...

  5. inline-block和float 布局的选择

    浮动通常表现正常,但有时候搞起来会很纠结.特别是处理内部容器中的浮动,比如对一排图片使用浮动后对齐出现问题.Inline-block是我们的另一种选择.使用这种属性可以模拟部分浮动的特征,而不需要处理 ...

  6. get函数

    dict={"name":"jary","age":22}print(dict.get("age")) # 通过键值找到 ...

  7. spring cloud 版本号与 boot版本之间的对应关系(版本不对,会导致pom无法引入)

    版本号规则 Spring Cloud并没有熟悉的数字版本号,而是对应一个开发代号. 开发代号看似没有什么规律,但实际上首字母是有顺序的,比如:Dalston版本,我们可以简称 D 版本,对应的 Edg ...

  8. 第 8 章 容器网络 - 063 - 如何使用 Weave 网络?

    如何使用 Weave 网络? weave 是 Weaveworks 开发的容器网络解决方案. weave 创建的虚拟网络可以将部署在多个主机上的容器连接起来. 对容器来说,weave 就像一个巨大的以 ...

  9. 『Python』源码解析_源码文件介绍

    本篇代码针对2.X版本,与3.X版本细节不尽相同,由于两者架构差别不大加之本人能力有限,所以就使用2.X体验python的底层原理了. 一.主要文件夹内容 Include :该目录下包含了Python ...

  10. Matlab:五点差分方法求解椭圆方程非导数边值问题

    差分格式脚本文件: tic; clear clc M=32;%x的步数 N=16;%y的步数 h1=1/M;%x的步长 h2=1/N;%y的步长 x=0:h1:1; y=0:h2:1; u=zeros ...