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. react中使用antd遇到的问题

    1.less使用报错 less配置修改一般都是1个修改1个增加 test: /\.(css|less)$/, // 修改 // 增加 { loader: require.resolve('less-l ...

  2. 【Visual Studio 扩展工具】如何在ComponentOne的DataTree中实现RightToLeft布局

    概述 C1FlexGrid提供了创建轮廓树的功能,其中可以显示缩进结构,每个节点行旁边都有折叠/展开图标. 然后,用户可以展开和折叠轮廓以查看所需的细节级别. 为此,C1FlexGrid允许您使用其T ...

  3. Ubuntu下 fatal error: Python.h: No such file or directory 解决方法

    参考: fatal error: Python.h: No such file or directory Ubuntu下 fatal error: Python.h: No such file or ...

  4. 没有显示器、网线、路由器,编辑TF卡连接树莓派

    只有电脑,连接树莓派的方法 电脑新建热点 打开TF卡,在根目录新建文件wpa_supplicant.conf,内容如下 country=GB ctrl_interface=DIR=/var/run/w ...

  5. linux存储管理之文件系统

    EXT3/4文件系统 ====================================================================================Ext3: ...

  6. android Studio 出现:Unable to resolve dependency for ':app@debug/compileClasspath'

    li经千辛万苦,我的新工程gradle搞定了 但是却在变异的时候告诉我 Unable to resolve dependency for ':app@debug/compileClasspath'xx ...

  7. python模块--re模块

    常用的正则表达式模式: .  匹配除换行符以外的任意字符 \d  匹配一个数字字符.等价于 [0-9]. \D  匹配一个非数字字符.等价于 [^0-9]. \s  匹配任何空白字符,包括空格.制表符 ...

  8. Andorid Studio中运行模拟器--夜神模拟器

    这样可以直接在夜神模拟器上运行app然后在androidstudio上查看log…. 1.下载夜神模拟器 2.修改配置 点击右上角的设置图标,对夜神模拟器的分辨率进行选择,手机版的480×800的就差 ...

  9. 若依项目整合eCharts实现图表统计功能

    eCharts是一款强大的图表统计工具,具体介绍可查看其官网 http://echarts.baidu.com/echarts2/index.html 下面记录一下如何在若依项目中使用eCharts. ...

  10. hdu多校第3场A.Ascending Rating

    Problem A. Ascending Rating Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Sub ...