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. get data from splunk

    link: http://dev.splunk.com/view/python-sdk/SP-CAAAER5 download SDK & setup with python code: im ...

  2. i++为什么不能作为左值,而++i可以作为左值

    今天看书见到如下代码: int a=2; ++a++; 根据操作符的优先级和结合性知,操作符++的优先级为3,结合性为右结合,即++a++;可以理解为++(a++); 但我把代码放在vs2015上,结 ...

  3. sed输出指定行

    and line ,8p to line ,8p -e 20p - and line -n:取消默认输出.注意:sed命令会默认把输入行打印到屏幕上,所以如果想精准的控制输出,就需要-n. -e:进行 ...

  4. 每天学点Python之collections

    每天学点Python之collections 内容摘抄自:<python大法好>的每天学点Python之collections collections模块在内置数据类型(dict.list ...

  5. C51 使用端口 个人笔记

    使用整个端口的8个引脚: 八个引脚,需要8位2进制,2位十六进制 #define P0 led led = 0x3f; //led = ~0x3f; 使用某个端口的某一个引脚 sbit led = P ...

  6. 牛客网sql练习

    一建表语句 /* Navicat MySQL Data Transfer Source Server : test Source Server Version : 50717 Source Host ...

  7. 【裸的并查集】POJ 1611 The Suspects

    http://poj.org/problem?id=1611 [Accepted] #include<iostream> #include<cstdio> #include&l ...

  8. JPA的一些坑

    推荐一篇比较好的介绍JPA的文章:使用 Spring Data JPA 简化 JPA 开发 JPA坑1:不支持Limit查询 JPA是不支持Limit分页查询,而我们有时又因为某些原因不想用JPA提供 ...

  9. Linux下汇编语言学习笔记5 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  10. POJ 1182_食物链

    题意: 三种动物A,B,C,A吃B,B吃C,C吃A, 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同类. 第二种说法是" ...