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. New需谨慎

    New is Glue When you’re working in a strongly typed language like C# or Visual Basic, instantiating ...

  2. jsp/servlet学习四之jsp初窥

    jsp页面本质上是一个servlet,jsp页面是一个以.jsp结尾的文本文件. jsp自带的API包含4个包: javax.servlet.jsp.包含用于servlet/jsp容器将jsp页面翻译 ...

  3. JS函数、变量作用域

    函数参数 函数的()中指定一个或多个形参(形式参数),多个形参之间用,号隔开,声明形参相当于在函数内部声明了对应的变量,但不赋值.在调用时在()中指定实参  调用时解析器不会检查实参类型.数量,实参可 ...

  4. VS工作目录,输出目录

    C++项目,解决方案总文件夹下就只包含解决方案配置文件sln和一个项目总文件夹和一个Debug文件夹以及一个Release文件夹(共四个东东,其中Debug和Release文件夹中存放最终生成的结果e ...

  5. 构建oracle12c的Docker镜像

    1. 准备工作 需要下载oracle相关的东东,例如安装文件,dockerfile.这些都可以从oracle 的github 上面找到.https://github.com/oracle/docker ...

  6. vue form表单验证

    <el-select v-model="ruleForm.region" placeholder="请选择活动区域"> <el-option ...

  7. php搜索附近人及显示男生女生分开

    // 滚动切换标签样式 switchTab: function (e) { this.setData({ currentTab: e.detail.current }); this.checkCor( ...

  8. ASP.NET后台输出js

    一.使用RegisterClientScriptBlock输出js代码块 StringBuilder sb = new StringBuilder(); sb.Append("<inp ...

  9. 再次提供一个纯粹通过pl/sql解析json的方法。

    在github上面有一个叫pljson的项目,该项目就是用pl/sql 来解析json的. 项目地址:pljson(需翻|强),如果翻不了强的同学,我在国内克隆了一个副本,不定期同步更新 pljson ...

  10. TreeTagger

    大家都知道TreeTagger是一个用于词性标注和词型还原的工具,它的返回结果由三部分组成:目标词汇,词性,原形.那该工具是怎样判断目标词汇的词性的呢?又是怎样得到目标词汇的原形的呢?本文就是从这两个 ...