B. Modulo Sum
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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).

Output

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.

Sample test(s)
Input
3 5
1 2 3
Output
YES
Input
1 6
5
Output
NO
Input
4 6
3 1 1 3
Output
YES
Input
6 6
5 5 5 5 5 5
Output
YES
Note

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 && 鸽巢原理 && 同余模)的更多相关文章

  1. Codeforces 1188C DP 鸽巢原理

    题意:定义一个序列的beauty值为序列中元素之差绝对值的最小值,现在给你一个数组,问所有长度为k的子序列的beauty值的和是多少? 思路:(官方题解)我们先解决这个问题的子问题:我们可以求出bea ...

  2. ACM数论之旅14---抽屉原理,鸽巢原理,球盒原理(叫法不一又有什么关系呢╮(╯▽╰)╭)

    这章没有什么算法可言,单纯的你懂了原理后会不会运用(反正我基本没怎么用过 ̄ 3 ̄) 有366人,那么至少有两人同一天出生(好孩子就不要在意闰年啦( ̄▽ ̄")) 有13人,那么至少有两人同一月 ...

  3. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  4. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  5. poj 2356 Find a multiple(鸽巢原理)

    Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...

  6. poj2356 Find a multiple(抽屉原理|鸽巢原理)

    /* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...

  7. Find the duplicate Number (鸽巢原理) leetcode java

    问题描述: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  8. POJ2356 Find a multiple 抽屉原理(鸽巢原理)

    题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...

  9. hdu 1205 吃糖果【鸽巢原理】

    题目 这道题不难,看别人博客的时候发现大家都说用鸽巢原理,这是个什么鬼,于是乎百度之. 1.把某种糖果看做隔板,如果某种糖果有n个,那么就有n+1块区域,至少需要n-1块其他种糖果才能使得所有隔板不挨 ...

随机推荐

  1. C#获取文件的Md5值

            private string GetMd5(Stream fileStream)         {             MD5CryptoServiceProvider md5P ...

  2. CF 405B Domino Effect(想法题)

    题目链接: 传送门 Domino Effect time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  3. CF 208A Dubstep(简单字符串处理)

    题目链接: 传送门 Dubstep Time Limit: 1000MS     Memory Limit: 32768 KB Description Vasya works as a DJ in t ...

  4. 【Alpha阶段】第七次Scrum例会

    会议信息 时间:2016.10.29 21:30 时长:60min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.11.01 21:30 个人任务报告 姓名 今日已完成Is ...

  5. 电脑开启wifi功能

    在Win7下,以管理员身份启动cmd,输入以下两条命令创建虚拟Wifi,并启动Wifi.其中ssid为WiFi的名字,key为密码.C:\>netsh wlan set hostednetwor ...

  6. Apache Tomcat目录下各个文件夹的作用

    1.bin:存放各种不同平台开启与关闭Tomcat的脚本文件. 2.lib:存tomcat与web应用的Jar包. 3.conf:存放tomcat的配置文件. 4.webapps:web应用的发布目录 ...

  7. ecshop 远程图片本地化

    define('IN_ECS', true); require(dirname(__FILE__) . '/includes/init.php'); $smarty->assign('siteD ...

  8. Cheminformatic Set

    蛋白: 数据库 1. 蛋白晶体结构数据库 http://www.rcsb.org/pdb/home/home.do 2. 蛋白注释数据库 http://www.uniprot.org/ 工具 1. r ...

  9. cobbler安装、部署、测试

    cobbler功能介绍 官网:http://cobbler.github.io 安装 yum install -y httpd tftp dhcp cobbler cobbler-web pykick ...

  10. 基本药目录sop

    http://db.yaozh.com/basicdir 基本药物 编辑 "基本药物"的概念, 由世界卫生组织于1977年提出,指的是能够满足基本医疗卫生需求,剂型适宜.保证供应. ...