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. JPA的Column注解总结

    就像@Table注解用来标识实体类与数据表的对应关系类似,@Column注解来标识实体类中属性与数据表中字段的对应关系. 该注解的定义如下: @Target({METHOD, FIELD}) @Ret ...

  2. NodeJS记录

    https://nqdeng.github.io/7-days-nodejs/#3.3.4

  3. 【转】完美解除Windows7的驱动程序强制签名限制

    原文网址:http://nick.txtcc.com/index.php/nocategory/290 Windows 7很J,很多驱动程序都无法安装,因为Windows 7不像Vista,必须要求所 ...

  4. fs event_socket

    mod_event_socket     Skip to end of metadata   Created by John Boteler, last modified by Niek Vlesse ...

  5. ADS1110/ADS1271

    ADS1110 1.初始化 软件:设置p任意2个为GPIO口 硬件:设置p0.2,p0.3为SDA,SCL 输入.输出 ADS1110的I2C地址(1001aaa)例如ADS1110A0的地址是100 ...

  6. 进程kswapd0与events/0消耗大量CPU的问题

    http://www.nowamagic.net/librarys/veda/detail/2539 今天下午网站宕了两次机,发工单给阿里云,发现原因是服务器的CPU 100%了. 重启服务器后,使用 ...

  7. EDIUS如何实现抠图

    有时候我们把素材图片导入到EDIUS中会发现图片的背景有点不合适,这时候就会想替换掉这个背景.那么EDIUS也可以像PS一样替换掉背景吗?答案当然是肯定的,现在小编就带领你们一起来学习EDIUS抠图吧 ...

  8. sql server 排序规则

    /*   排序规则根据特定语言和区域设置的标准指定对  字符串  数据 进行排序和比较的规则.   以 ORDER BY 子句为例:如果按升序排列,说英语的人认为字符串 Chiapas 应排在 Col ...

  9. css @语法,@规则 @import @charset @font-face @fontdef @media @page

    CSS At-Rules Reference    样式表规则 At-Rules 样式表规则 CSS Version 版本 Compatibility 兼容性 Description 简介 @impo ...

  10. linux服务之crond

    use the following command add entries to crontab should take effect right away. right away(立即,立刻) #c ...