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

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

题意:

取任意个数的和能否组成M的倍数;宇神用背包写的,参谋了下,很聪明的解法,还可以用set做;

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1e3 + ;
int num[MAXN * MAXN];
int v[MAXN * MAXN];
int dp[ * MAXN][MAXN];
int val[MAXN * MAXN];
int main(){
int n, m;
while(~scanf("%d%d", &n, &m)){
memset(num, , sizeof(num));
memset(v, , sizeof(v));
memset(dp, , sizeof(dp));
memset(val, , sizeof(val));
int tp = ;
int temp;
int ans = ;
for(int i = ; i < n; i++){
scanf("%d", &temp);
if(temp == m){
ans = ;
}
if(!num[temp % m])
v[tp++] = temp % m;
num[temp % m]++;
}
n = tp;
/*
printf("tp = %d\n",tp);
for(int i = 0; i < tp; i++){
printf("v = %d num = %d \n", v[i], num[v[i] ]);
}puts("");
*/
tp = ;
for(int i = ; i < n; i++){
for(int j = ; j <= num[v[i]]; j <<= ){
if(j * v[i] % m == ){
ans = ;
}
num[v[i]] -= j;
val[tp++] = j * v[i] % m;
}
if(num[v[i]] > ){
if(v[i] * num[v[i]] % m == )
ans = ;
val[tp++] = v[i] * num[v[i]] % m;
}
}
/*
printf("tp = %d\n",tp);
for(int i = 0; i < tp; i++){
printf("v = %d num = %d \n", val[i], num[v[i] ]);
}puts("");
*/
dp[][v[]] = ;dp[][] = ;
// printf("ans = %d\n",ans);
for(int i = ; i < tp - ; i++){
for(int j = ; j <= m; j++){
if(dp[i][j]){
// printf("i = %d j = %d\n",i,j);
dp[i + ][j] = ;
if((j + val[i + ]) % m == ){
ans = ;
}
dp[i + ][(j + val[i + ]) % m] = ;
}
}
}
if(ans)puts("YES");
else puts("NO");
}
return ;
}

set:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;
const int MAXN = 1e6 + ;
int main(){
int n, m;
while(~scanf("%d%d", &n, &m)){
int temp, ans = ;
set<int>st, _st;
st.insert();
set<int>::iterator iter;
for(int i = ; i < n; i++){
scanf("%d", &temp);
for(iter = st.begin(); iter != st.end(); iter++){
if((*iter + temp) % m == ){
printf("YES\n");
return ;
}
_st.insert((*iter + temp) % m);
}
st.insert(_st.begin(), _st.end());
_st.clear();
}
puts("NO");
}
return ;
}

vector:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
const int MAXN = 1e6 + 100;
int vis[MAXN];
int main(){
int n, m;
while(~scanf("%d%d", &n, &m)){
int temp, ans = 0;
vector<int>st, _st;
st.push_back(0);
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; i++){
scanf("%d", &temp);
if(ans)continue;
for(int i = 0; i < st.size(); i++){
int x = (st[i] + temp) % m;
if(x == 0){
ans = 1;
break;
}
if(!vis[x])vis[x] = 1,_st.push_back(x);
}
for(int i = 0; i < _st.size(); i++){
st.push_back(_st[i]);
}
_st.clear();
}
if(ans)puts("YES");
else puts("NO");
}
return 0;
}

  

Modulo Sum(背包 + STL)的更多相关文章

  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

                                                             B. Modulo Sum                               ...

  4. cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. codeforces 577B B. Modulo Sum(水题)

    题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. CF577B Modulo Sum 好题

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. Codeforces 577B Modulo Sum

    http://codeforces.com/problemset/problem/577/B 题意:有n个数,求有无一个子序列满足和是m的倍数 思路:用模下的背包做,发现n是十的六次方级别,但是有个神 ...

  8. 【Henu ACM Round#18 B】Modulo Sum

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] m比较小 <=1000 a[i]直接看成a[i]%m就可以了. 有n个0..999之间的整数.. 如果有一个0那么就直接输出Y ...

  9. codeforces 577B. Modulo Sum 解题报告

    题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...

随机推荐

  1. 在Struts2中使用Uploadify组件上传文件

    Uploadify是一个基于Jquery的文件上传组件,官网http://www.uploadify.com/可以在官网获得该组件,运行演示示例,下载帮助文档.     作为Web前端的增强技术,Jq ...

  2. Linux系统守护进程详解ntsysv 可以关掉那些服务

    acpid, haldaemon, messagebus, klogd,network, syslogd  以上几个服务必须开启!其他的分析如下: 1.NetworkManager,NetworkMa ...

  3. Qt快速入门系列教程目录

    Qt快速入门系列教程目录

  4. 该如何关闭thinkphp的缓存呢?有下面几种方法可参考:

    该如何关闭thinkphp的缓存呢?有下面几种方法可参考: (1)在配置文件中关闭缓存 在你的配置文件config.php文件中加上如下两句:   复制代码代码如下: 'TMPL_CACHE_ON'  ...

  5. MySql命令——show,分页,正则表达式

    先要安装MySql,过程见 MySql5.1在Win7下的安装与重装问题的解决 不是教程,还没有写教程的资格,只是为了自己查阅而已!   show show databases; //显示所有数据库 ...

  6. CSS3 target伪类简介

    CSS3 target伪类是众多实用的CSS3特性中的一个.它用来匹配文档(页面)的URI中某个标志符的目标元素.具体来说,URI中的标志符通常会包含一个”#”字符,然后后面带有一个标志符名称,比如# ...

  7. bower安装使用以及git安装

    bower需要:node 和 git node安装包下载:http://blog.csdn.net/myan/article/details/2028545 Git安装: 选择第二项:Use Git ...

  8. Dreamwaver 使用root用户连接不上远程服务器

    我用dreamweaver连接远程服务,开始用的是root用户登录的,但是连接不上.网上查了一下,解决教程非常复杂,我就不列出来了. 后来我想了一下,之前我有连接过.我感觉可能是用户的问题,于是我在远 ...

  9. Android 使用开源xUtils来实现多线程下载(非原创)

    1.程序员自己也是可以实现多线程下载的,只是代码量比较大,而且,其中有许多细节需要考虑到,在GitHub上有人写好的代码,我们可以拿过来使用下,节省了我们开发程序的时间 2.导包:xUtils-2.6 ...

  10. Ubuntu eclipse 命令补全失效 (转载)

    我的eclipse 3.4,从ibm网站上下载解压后使用.发觉自动补全功能(alt + /)失效. 解决的办法: 1.(eclipse)window --> preferences --> ...