hdu 5776 抽屉定理
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 抽屉定理的更多相关文章
- HDU 5776 sum(抽屉原理)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=5776 Problem Description Given a sequence, you're ask ...
- HDU 5776 sum( 鸽巢定理简单题 )
链接:传送门 题意:给一个长为 n 的串,问是否有子串的和是 m 的倍数. 思路:典型鸽巢定理的应用,但是这里 n,m 的大小关系是不确定的,如果 n >= m 根据定理可以很简单的判定是一定有 ...
- HDU 5776 sum (模拟)
sum 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5776 Description Given a sequence, you're asked ...
- HDU 5776 sum (思维题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 题目让你求是否有区间的和是m的倍数. 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续 ...
- HDU 5776
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 求是否有区间的和是m的倍数 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续子列可以组 ...
- HDU 3037(Lucas定理)
对于很大的组合数不能用C(n, m) = C(n - 1, m) + C(n-1, m -1)来求,这里就用到Lucas定理. 模板题: hdu3037:模板如下: #include <cstd ...
- 【抽屉定理】 组合数学poj2356
假定n个数为a1,a2,...,an,前n项和分别是S1.S2.....Sn,那么如果有一个Si模n是0,就是答案,否则,n个数模n的余数只能在 1到n - 1之间,把余数作为抽屉,显然n个数放到n ...
- hdu 3547 (polya定理 + 小高精)
DIY CubeTime Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4671 Partition(定理题)
题目链接 这题,明显考察搜索能力...在中文版的维基百科中找到了公式. #include <cstdio> #include <cstring> #include <st ...
随机推荐
- .NET ActiveMQ类库
ActiveMQ .NET类库 ActiveMQ是一种开源的,实现了JMS规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信. 0. 准备 使用Nuget ...
- C#三层架构
C#三层架构 三层架构分为:表现层(UI(User Interface)).业务逻辑层(BLL(Business Logic Layer)).数据访问层(DAL(Data Access Layer)) ...
- js设置、读取、删除cookie
设置cookie: function setCookie(oJson , time){ var data = new Date( new Date().getTime() + time*24*60*6 ...
- 依赖注入demo
让我们看一个例子: class UserProvider{ protected $connection; public function __construct(){ $this->connec ...
- WEB UI做TREE
效果图: 原本的普通搜索帮助,改成上面这样层级的搜索帮助.这里只做了两级. 一,新建一个TREE节点 1.新建tree结构:ZGRTEXT 2.新建树叶节点处理类: 修改超类为CL_BSP_WD_TR ...
- 封装一个使用cURL以POST方式请求https协议的公众方法
打开php7.2手册,搜索curl function getRequest($url,$type='get', $data = [], $timeout = 10) (需要更改){ $ssl = st ...
- React Native 开发日常、常见问题总结及解决
优点: 1.写 UI 快,跟写 HTML 差不多,flex 布局写起来很爽,而且跨平台: 2.调试方便,command + R 直接刷新 Simulator,不用像 Xcode 等待编译: 3.体验好 ...
- html5(三)
body{ text-align:center;} #mainbox { display:block; width:500px; margin:50px auto; padding:15px; tex ...
- Python—装饰器详解
装饰器:(语法糖) 本质是函数,它是赋予函数新功能,但是不改变函数的源代码及调用方式 原则: 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 3.函数的返回值也不变 这两点简而言 ...
- Python3版本中的filter函数,map函数和reduce函数
一.filter函数: filter()为已知的序列的每个元素调用给定的布尔函数,调用中,返回值为非零的元素将被添加至一个列表中 def f1(x): if x>20: return True ...