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 by6, 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.

题意:

给出一个数列,和一个数m,问能不能从这个数列中选出若干个数,使得这些数的和可以整除m

整除m,也就是%m==0

其实是个很水的01背包,每个数取和不取

dp[i][j]表示选择到第i个数,和模m==j的情况有没有(有1,没有0)

但是我们会发现n很大,m很小

根据抽屉原理,当n>=m时,一定能

当n<m时,此时的数据大小<=1000,这个时候的dp,复杂度为n*m<m*m,可以过了

#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int maxm=1e3+;
const int maxn=1e6+; int dp[maxm][maxm];
int a[maxn]; int main()
{
int n,m; scanf("%d %d",&n,&m);
for(int i=;i<=n;++i){
scanf("%d",&a[i]);
a[i]%=m;
}
if(n>=m){
printf("YES\n");
return ;
} memset(dp,,sizeof dp); for(int i=;i<=n;i++){
dp[i][a[i]]=;
for(int j=;j<m;j++){
if(dp[i-][j]){
dp[i][j]=true;
dp[i][(j+a[i])%m]=true;
}
}
} if(dp[n][]>)
printf("YES\n");
else
printf("NO\n"); return ;
}

CF577B Modulo Sum 好题的更多相关文章

  1. codeforces 577B B. Modulo Sum(水题)

    题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  4. Modulo Sum(背包 + STL)

     Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. Codeforces Round #319 (Div. 2) B. Modulo Sum 抽屉原理+01背包

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #319 (Div. 2)B. Modulo Sum DP

                                                             B. Modulo Sum                               ...

  7. Codeforces 577B Modulo Sum

    http://codeforces.com/problemset/problem/577/B 题意:有n个数,求有无一个子序列满足和是m的倍数 思路:用模下的背包做,发现n是十的六次方级别,但是有个神 ...

  8. 【CodeForces 577B】Modulo Sum

    题 题意 给你n(1 ≤ n ≤ 106)个数a1..an(0 ≤ ai ≤ 109),再给你m( 2 ≤ m ≤ 103)如果n个数的子集的和可以被m整除,则输出YES,否则NO. 分析 分两种情况 ...

  9. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

随机推荐

  1. UVa 1584 Circular Sequence --- 水题

    UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...

  2. UVALive4287 hdu2767 hdu3836 强连通

    题意:有多个命题,需要证明他们可以互相推出,现在已经有一些证明关系即 A 可以证明 B,问至少还需要多少证明关系. 首先,如果某几个命题证明关系可以成环,那么这些命题必然可以相互证明,只要沿着环的边走 ...

  3. poj2912 带权并查集

    题意:多个人玩石头剪刀布,每个人提前选定了自己出哪个手势,而其中有一种特殊的人他可以随意出什么手势,问是否能够从给出的一系列石头剪刀布游戏中判断出哪个是特殊的,可以从第几局游戏中判断出来. 首先按照食 ...

  4. knockoutJs基础1 - 简单的knockoutjs实现

    简单的knockoutjs实现 1.knockoutJs是在MVVM的机制下实现的,所以要有view(HTML页面中的DOM标签)和viewModel(JavaScript中的js代码). 2.在vi ...

  5. Windows7下QT5开发环境搭建 分类: QT开发 2015-03-09 23:44 65人阅读 评论(0) 收藏

    Windows7下QT开法环境常见搭配方法有两种. 第一种是:QT Creator+QT SDK: 第二种是:VS+qt-vs-addin+QT SDK: 以上两种均可,所需文件见QT社区,QT下载地 ...

  6. Netty系列之Netty可靠性分析

      作者 李林锋 发布于 2014年6月19日 | 29 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件分享 稍后阅读 我的阅读清单   1. 背景 1.1. 宕机的代价 1.1. ...

  7. sql防注入的简单实现,防XSS的简单实现

    1.sql-替换'(切断字符串)符和\(转义字符)符为空, 2.xss-替换<(标签开始符)符 但用这种简单方法在sql和html中不能再使用这些字符了.

  8. vb6通send和recv请求网络资源

    最近为了弄清楚send和recv的用法,特意用vb6测试了一下,头文件冗余的比较多: Option Explicit Private Declare Function setsockopt Lib & ...

  9. Spring3.1新属性管理API:PropertySource、Environment、Profile

    Spring3.1提供了新的属性管理API,而且功能非常强大且很完善,对于一些属性配置信息都应该使用新的API来管理.虽然现在Spring已经到4版本了,这篇文章来的晚点. 新的属性管理API Pro ...

  10. oracle组件

    目前在用的四个oracle版本 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Parti ...