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. 编写linux驱动所用到的头文件(转)

    转自:http://blog.csdn.net/lufeiop02/article/details/6448497 关于linux驱动(应用)程序头文件使用 收藏 驱动程序: #include < ...

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

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

  3. log4j 使用笔记整理中

    Log4j由三个重要的组件构成:日志信息的优先级,日志信息的输出目的地,日志信息的输出格式. 日志信息的常用的优先级从高到低有ERROR.WARN. INFO.DEBUG,分别用来指定这条日志信息的重 ...

  4. eclipse中新建maven项目-转

    http://huxiaoheihei.iteye.com/blog/1766986 博客分类: maven maven  maven是个项目管理工具,集各种功能于一身,下面介绍maven web项目 ...

  5. proBuilder编辑的模型变黑

    ----更正: 旧帖中方法有误,解决不了问题. 更正确法: 将proBuilder创建的模型的Static属性由“-”改为去掉勾选:   ----旧帖 proBuilder编辑的模型变黑解法: 1,U ...

  6. vs2012 Nuget错误:“未能解析此远程名称api.nuget.org”

    今天在使用vs2012的Nuget功能向新建MVC项目中添加bootstrap时出现一个错误:“未能解析此远程名称api.nuget.org”: 网上查阅资料得知:nuget.org使用的godadd ...

  7. java中的分支

    条件语句:     if-else选择结构:         if(条件块){             代码块1         }else{             代码块2         }   ...

  8. ASP.NET 4.0的ClientIDMode属性

    时光流逝,我们心爱的ASP.NET也步入了4.0的时代,微软在ASP.NET 4.0中对很多特性做了修改.比如我将要讨论的控件ID机制就是其中之一. 在ASP.NET 4.0之前我们总是要为控件的Cl ...

  9. 读取Config文件工具类 PropertiesConfig.java

    package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...

  10. 打印Java main方法执行的命令参数代码

    RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean(); List<String> jvmArgs = bean.getInpu ...