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. 重载operator new实现检测内存泄漏是否可行

    行与不行,就凭我这水平,说出来未免显示太过自大.不还,我还想根据自己的代码来讨论这个问题. 重载operator new来检测内存只的办法,那就是在new的时候记录指针地址及文件名.行号,在delet ...

  2. xmind教程

    xmind是什么东西我不多说.作为一个程序员,我通常用来编写一个文档.比如某个模块的设计或者流程图. 一开始我是以word画图的方式来用xmind的,即想要什么图形,就去插入里面找.结果碰了一鼻子灰, ...

  3. Unity 屏幕适配小脚本

    屏幕适配是可以通过代码实现的,相信给你时间就一定能写出来. 我们公司貌似没有分辨率适配框架通常对应小屏幕的苹果4要额外设置下等等就完了! 屏幕适配框架实现思路:  通过代码获取当前的分辨率 –> ...

  4. 简易的C/S系统(实现两个数的和)

    //Client:#include <string.h> #include <sys/socket.h> #include <stdio.h> #include & ...

  5. SqlDependency不起作用

    今天使用SqlDependency,结果不起作用,失效,不管数据库怎么修改,这边都没反应,OnChange事件总是不执行,很奇怪.我打开msdn里的例子,代码复制出来,结果没问题,能执行,那剩下来的问 ...

  6. PHP连接sql server 2005环境配置

    一.Windows下PHP连接SQLServer 2005 设定:安装的Windows操作系统(Win7 或XP均可.其它系统暂未測试),在C盘下:PHP的相关文件位于c:/PHP以下,其配置文件ph ...

  7. EEPlat vs saleforce 配置 Knowledge Article 演示样例

    ==================================================================================================== ...

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

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

  9. Javascript - IE8下parseInt()方法的取值异常

      公司的测试小妹妹跑来对我说,下拉框第9项始终无法正确提交的时候,我还以为见鬼了.   parseInt()会把'0'开头的数字以8进制来解析,当有大于7的数字时候就按10进制来解析.   // p ...

  10. Visual Studio 2013 Web开发、新增功能:“Browser Link”

    微软正式发布Visual Studio 2013 RTM版,微软还发布了Visual Studio 2013的最终版本..NET 4.5.1以及Team Foundation Server 2013. ...