B. Modulo Sum
                                                                                                 time limit per test

2 seconds

                                                                                                 memory limit per test

256 megabytes

 

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

题意:给你n,m,n个数,让你从中找出任意数的和mod M==0

题解:背包dp

//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define maxn 1000005
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//****************************************
int hashs[maxn],a[maxn*];
bool dp[][maxn];
int main()
{ mem(hashs);
int flag=;
int n=read(),m=read();
FOR(i,,n)
{
scanf("%d",&a[i]);
a[i]=a[i]%m;
if(a[i]==)a[i]=m;
}dp[][]=true;
dp[][]=true;
for(int i=;i<=n;i++)
{
for(int j=m;j>=;j--)
{
if(dp[][j])
{
if(j+a[i]==m){
puts("YES");
return ;
}
dp[][(j+a[i])%m]=true;
}
}
memcpy(dp[],dp[],sizeof(dp[]));
}
cout<<"NO"<<endl;
return ;
}

代码君

Codeforces Round #319 (Div. 2)B. Modulo Sum DP的更多相关文章

  1. 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/ ...

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

  3. Codeforces Round #319 (Div. 2) B Modulo Sum (dp,鸽巢)

    直接O(n*m)的dp也可以直接跑过. 因为上最多跑到m就终止了,因为前缀sum[i]取余数,i = 0,1,2,3...,m,有m+1个余数,m的余数只有m种必然有两个相同. #include< ...

  4. Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...

  5. Codeforces Round 319 # div.1 & 2 解题报告

    Div. 2 Multiplication Table (577A) 题意: 给定n行n列的方阵,第i行第j列的数就是i*j,问有多少个格子上的数恰为x. 1<=n<=10^5, 1< ...

  6. Codeforces Round #319 (Div. 2)

    水 A - Multiplication Table 不要想复杂,第一题就是纯暴力 代码: #include <cstdio> #include <algorithm> #in ...

  7. Codeforces Round #302 (Div. 2).C. Writing Code (dp)

    C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard inp ...

  8. Codeforces Round #338 (Div. 2) C. Running Track dp

    C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...

  9. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

随机推荐

  1. Android首页轮播图直接拿来用

    import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executors; import jav ...

  2. 数据库insert update select语句

    http://database.51cto.com/art/200903/113939_1.htm                   (更新语句) http://blog.csdn.net/chan ...

  3. 笔试算法题(25):复制拥有多个指针的链表 & 判断二元树B是否为A的子树

    出题:定义一个复杂链表:在单向链表的基础上,每个节点附加一个指向链表中其他任意节点的指针sibling,实现CNode* Clone(Cnode *head)函数复制这个复杂链表: 分析: 解法1:将 ...

  4. linux下nginx、php和mysql安装配置

    一.安装nginx 安装nginx yum install -y epel-release yum install nginx -y 查看nginx软件包包括了哪些文件 rpm -ql nginx 启 ...

  5. html页面加载初始化方法

    js: 方法一: window.onload=function(){内容} 方法二(自己定义方法): function onload(){内容} onload(); jQuery: 方法一: $(do ...

  6. 【BZOJ 1202】 [HNOI2005]狡猾的商人(枚举区间也可行)

    题链:http://www.lydsy.com/JudgeOnline/problem.php?id=1202 其实也可以不使用加权并查集,通过画图可以发现,一个长区间和其包含的区间能够算出一个新区间 ...

  7. POJ-20407Relatives/NYOJ-333mdd的烦恼,欧拉函数简单应用,模板A

     poj                         Relatives                                Time Limit: 1000MS   Memory Li ...

  8. spark streaming基于Kafka的开发

    spark streaming使用Kafka数据源进行数据处理,本文侧重讲述实践使用. 一.基于receiver的方式 在使用receiver的时候,如果receiver和partition分配不当, ...

  9. Win 2003 创建 IP 安全策略来屏蔽端口的图文教程

    (本文用示例的方法讲解 IP 安全策略的设置方法,具体的设置还是要根据个人实际的需要来设置.另外 Windows Server 2008 与此类似.千一网络编辑注) IP安全性(Internet Pr ...

  10. 安装K/3 Cloud过程中发现的两个新问题。

    卸载掉K/3 Cloud然后重装时出现下面的错误提示: 可能原因: 1.安装目录下的Setup.exe会检查操作系统版本.有些操作系统可能是被串改过注册信息,所以取不到版本信息(有些是因为盗版的原因) ...