cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)
2 seconds
256 megabytes
standard input
standard output
You are given a sequence of numbers a1, a2, ..., an, and a number m.
Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m.
The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.
3 5
1 2 3
YES
1 6
5
NO
4 6
3 1 1 3
YES
6 6
5 5 5 5 5 5
YES
In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.
In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.
In the third sample test you need to choose two numbers 3 on the ends.
In the fourth sample test you can take the whole subsequence.
鸽巢原理,不管怎么排,我们对数列求前缀和,在求模后得到pre1 , pre2 ,pre3 ,……pren,因为n>m,必存在prei = prej (i != j), 然后 (prei - prej) = 0 (mod m) 。
所以n>m时,为O(1)
然后剩下的部分n<=m时,用O(m*m)的dp即可。
然而没有我没有考虑鸽巢原理,用O(n*m)的dp加剪枝也过了,,,,,写dp要养成一个好习惯,那就是用当前的 已知解 去推 未知解 ,(反过来的话会碰上一些意想不到的问题),并且这样会自然形成一个剪枝
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
const int M = 1e6 + 10 ;
int vis[1000 + 10] ;
ll pre[M] ;
int n , m ;
int dp[1000 + 10] ;
int d[1000 + 10] ; int main () {
scanf ("%d%d" , &n , &m) ;
bool flag = 0 ;
for (int i = 1 ; i <= n ; i ++) {
int x ;
scanf ("%d" , &x) ;
x = x % m ;
if (x == 0) flag = 1 ;
vis[x] ++ ;
}
if (flag) {
puts ("YES") ;
return 0 ;
}
int tmp = -1 ;
dp[0] = 1 ;
while (vis[++tmp] == 0 && tmp <= 1000) ;
//printf ("%d : num(%d)\n" , tmp , vis[tmp]) ;
for (int i = 1 ; i <= vis[tmp] ; i ++) {
int ans = tmp*i%m ;
if (ans == 0) ans = m ;
dp[ans] = m ;
}
if (dp[m]) {
puts ("YES") ;
return 0;
}
for (int i = tmp+1 ; i <= m ; i ++) {
if (vis[i] == 0) continue ;
//printf ("%d : num(%d)\n" , i , vis[i]) ;
for (int i = 0 ; i <= m ; i ++) d[i] = dp[i] ;
for (int j = 0 ; j <= m ; j ++) {
if (d[j] == 0) continue ;
for (int k = 1 ; k <= vis[i] ; k ++) {
int ans = (j+i*k)%m ;
if (ans == 0) ans = m ;
dp[ans] = 1 ;
}
}
if (dp[m]) {
puts ("YES") ;
return 0 ;
}
}
//for (int i = 1 ; i <= m ; i ++) printf ("%d " , dp[i]) ; puts ("") ;
puts ("NO") ;
return 0 ;
}
cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)的更多相关文章
- Codeforces 1188C DP 鸽巢原理
题意:定义一个序列的beauty值为序列中元素之差绝对值的最小值,现在给你一个数组,问所有长度为k的子序列的beauty值的和是多少? 思路:(官方题解)我们先解决这个问题的子问题:我们可以求出bea ...
- ACM数论之旅14---抽屉原理,鸽巢原理,球盒原理(叫法不一又有什么关系呢╮(╯▽╰)╭)
这章没有什么算法可言,单纯的你懂了原理后会不会运用(反正我基本没怎么用过 ̄ 3 ̄) 有366人,那么至少有两人同一天出生(好孩子就不要在意闰年啦( ̄▽ ̄")) 有13人,那么至少有两人同一月 ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- poj 2356 Find a multiple(鸽巢原理)
Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
- Find the duplicate Number (鸽巢原理) leetcode java
问题描述: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- POJ2356 Find a multiple 抽屉原理(鸽巢原理)
题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...
- hdu 1205 吃糖果【鸽巢原理】
题目 这道题不难,看别人博客的时候发现大家都说用鸽巢原理,这是个什么鬼,于是乎百度之. 1.把某种糖果看做隔板,如果某种糖果有n个,那么就有n+1块区域,至少需要n-1块其他种糖果才能使得所有隔板不挨 ...
随机推荐
- 网络IO模型:同步IO和异步IO,阻塞IO和非阻塞IO
同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不同的人给出 ...
- 我总结的js方面你可能不是特别清楚的小知识
!!将一个值方便快速转化为布尔值 console.log( !!window===true ); 不声明第三个变量实现交换 var a=1,b=2; a=[b,b=a][0];//执行完这句代码之后 ...
- 捉襟见肘之TableView的手势(删除、编辑等)与转场动画手势冲突
在使用PresentModel的方式进行转场动画时,出现UIPercentDrivenInteractiveTransition和 UITableView的自带手势冲突,问题需要总结,今天系统复习和总 ...
- POJ3208:Apocalypse Someday
传送门 很神奇的一道题,正解是AC自动机+数位DP,个人感觉POPOQQQ大爷的方法更方便理解. 按照一般套路,先搞个DP预处理,设$f[i][0/1/2/3]$分别表示对于$i$位数,其中有多少个前 ...
- zabbix监控模式、分布式、自动化
适用场景: 1.监控主机多,性能瓶颈 2.多机房,防火墙 zabbix监控模式 针对agent来说 - 被动模式 - 主动模式(主动汇报服务端) 1)当监控主机超过300台,建议使用主动模式 2)当队 ...
- 【原】web移动端常用知识点笔记
摘要:因为平时搞移动端的比例多一点,做个小小的总结.虽然网上很多这方面的总结,不过还是想自己也总结一下,适合自己的才是最好的.这样也方便以后自己的查阅 viewport模板——通用 <!DOCT ...
- PHPCMS修改管理栏目下的模版设置的注意
要确保文件名后缀的统一才能被后台所找到 首页的必须是index开头.html结尾栏目首页的模板必须category开头.html结尾 -------例如导航栏上面的栏目页面 列表页的模板必须list开 ...
- 转:netflix推荐系统竞赛
原文链接:Netflix recommendations: beyond the 5 stars (Part 1), (Part 2) 原文作者:Xavier Amatriain and Justin ...
- linux常用快捷键
linux常用快捷键:ctrl+c 强制终止当前命令ctrl+l 清屏ctrl+a 光标移动到命令行首ctrl+e 光标移动到命令行尾ctrl+u 从光标合所在的位置删除到行首ctrl+z 把命令放到 ...
- ubuntu14.04安装了im-switch后系统设置中不见了语言支持
sudo apt-get install language-selector-gnome