B. Modulo Sum
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.

Examples
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
题意:从n个数中选取任意个数(最少一个)使得对m取模为0;
思路:首先当n>m的时候,是必定的;
    根据抽屉原理,前缀和必定有两个相等的数;sl==sr;
    sr-sl=0;意思就是[l,r]的和%m==0;
  n<m时,利用01背包,复杂度n*m;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e3+,M=1e6+,inf=1e9+;
int a[M];
int dp[N][N];
int max(int x,int y,int z)
{
return max(x,max(y,z));
}
int main()
{
int x,y,z,i,t;
memset(dp,,sizeof(dp));
scanf("%d%d",&x,&y);
for(i=;i<x;i++)
scanf("%d",&a[i]);
if(x>y)
{
printf("YES\n");
return ;
}
for(i=;i<x;i++)
dp[i][(a[i]%y)]=;
for(i=;i<x;i++)
{
for(t=;t<y;t++)
{
dp[i][t]=max(dp[i][t],dp[i-][t]);
dp[i][(t+a[i])%y]=max(dp[i-][t],dp[i][(t+a[i])%y]);
}
}
if(dp[x-][])
printf("YES\n");
else
printf("NO\n");
return ;
}

Codeforces Round #319 (Div. 2) B. Modulo Sum 抽屉原理+01背包的更多相关文章

  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 DP

                                                             B. Modulo Sum                               ...

  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. 【DP】:CF #319 (Div. 2) B. Modulo Sum

    [题目链接]:http://codeforces.com/contest/577/problem/B [相似题目]:http://swjtuoj.cn/problem/2383/ [题意]:给出n个数 ...

  8. Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

    E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...

  9. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

随机推荐

  1. DGbroker快速失败转移

    1.先决条件 DGMGRL> ENABLE FAST_START FAILOVER; Error: ORA-: requirements not met for enabling fast-st ...

  2. python中的configparser类

    Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置),所以可以自己写一个函数,实现读取config配置. config文件的写法比较简单,[sect ...

  3. php程序中判断session过期

    我们最常用的是在php程序中设置,如下例程序所示: if(!isset($_SESSION['abc']) || time()-$_SESSION['abc']>60){ $_SESSION[' ...

  4. 170508、忘记jenkins密码或者修改jenkins密码

    刚配置好jenkins时,不知道密码是什么,很多同学都有这种烦恼把,各种抓狂. 操作步骤: 1.进入jenkins用户目录  cd /home/rick/.jenkins/users/admin ps ...

  5. hello gradle

    首先下载和安装gradle可以参考官网下载地址,建议下载带有源码和文档的,以便后期查阅. 下载完以后打开终端输入gradle -v有如下信息输出,表示安装成功: bogon:gradle scott$ ...

  6. CentOS7使用yum安装nginx

    CentOS默认没有nginx的yum源需要yum安装nginx可以使用一下方法 一,环境检测 二,设置yum源 rpm -Uvh http://nginx.org/packages/centos/7 ...

  7. java 标识符与变量

    一.Java 标识符三要素 1.标识符由字母.下划线(_).美元符号($)或者字母组成. 2.标识符应以字母.下划线(_).美元符开头. 3.标识符字符大小写敏感,长度无限制. 标识符最重要的就是 见 ...

  8. Python开发【Django】:图片验证码、KindEditor

    图片验证码 生成图片验证码需要以下: session check_code.py(依赖:Pillow,字体文件) 模块安装 pip install Pillow src属性后面加? 在utils下拷贝 ...

  9. 怎么在Linux上抓包分析

    怎么在Linux上抓包分析 1.在Linux上抓包 例如在Ubuntu上,用命令抓包, tcpdump tcp  -i any -s0 -w desk.cap 用  sz desk.cap  把数据包 ...

  10. mysql 数据操作 单表查询 group by 聚合函数 没有group by情况下

    聚合函数只能用在组里使用 #没有group by 则默认算作一组 取出所有员工的最高工资 mysql> select max(salary) from employee; +---------- ...