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. 演示get、post请求如何算sn,算得sn如何使用

    import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncoding ...

  2. hibernate基础增删查改简单实例

    hibernate 基础理论知识网上很多,可以百度和google.这里不做多的介绍,以一个User表来开展例子 建一个web-project 我这里用了junit单元测试环境来进行增删查改的测试,别的 ...

  3. K米APP案例分析

    关于 K米 -- 的案例分析 产品 K米的APP (全国KTV点歌,手机直播,互动,交友,预订)的Android客户端 第一部分 调研,评测 评测: 软件的bug,功能评测,黑箱测试 • 下载并使用, ...

  4. DNS(企业级)

    构建DNS(企业级) 1.硬件选型 CPU:12C以上配置 内存:16G 网络:千兆 2.初始化系统配置 关闭 iptables service iptables stop chkconfig ipt ...

  5. 【原】javascript最佳实践

    摘要:这篇文章主要内容的来源是<javascript高级程序设计第三版>,因为第二遍读完,按照书里面的规范,发觉自己在工作中没有好好遵守.所以此文也是对自己书写js的一种矫正. 1.可维护 ...

  6. glade2支持C++代码的输出(2)

    今天更新了一个BaseObject的代码:BaseObject.002.zip 同时将glade2生成C++代码的代码进行了调整,基于2.12.2的补丁为:cpp_out_2.patch.tar.xz ...

  7. 获取字符串中每个字符出现的次数(利用TreeMap)

    案例:"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)分析1:定义一个字符串(可以改进为键盘录入)2:定义一个 ...

  8. C#------如何取出exe运行文件给客户使用

    1.将解决方案配置里面的“Debug”转换成“Release” 2.右击“解决方案”,选着“重新生成解决方案”,以得到最新的版本 3.找到工程目录下的“bin”文件夹,里面有“Release”文件夹, ...

  9. 导出excel失败,提醒提示加载类型库/DDL出错

    导出excel失败,提醒提示加载类型库/DDL出错 www.MyException.Cn   发布于:2012-08-17 02:08:34   浏览:1538次   导出excel失败,提示提示加载 ...

  10. C# Pointer types

    https://msdn.microsoft.com/en-us/library/y31yhkeb.aspx