Modulo Sum(背包 + STL)
2 seconds
256 megabytes
standard input
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.
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).
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.
3 5 1 2 3
YES
1 6 5
NO
4 6 3 1 1 3
YES
6 6 5 5 5 5 5 5
YES
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)的更多相关文章
- 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/ ...
- 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 ...
- Codeforces Round #319 (Div. 2)B. Modulo Sum DP
B. Modulo Sum ...
- cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)
B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforces 577B B. Modulo Sum(水题)
题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF577B Modulo Sum 好题
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces 577B Modulo Sum
http://codeforces.com/problemset/problem/577/B 题意:有n个数,求有无一个子序列满足和是m的倍数 思路:用模下的背包做,发现n是十的六次方级别,但是有个神 ...
- 【Henu ACM Round#18 B】Modulo Sum
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] m比较小 <=1000 a[i]直接看成a[i]%m就可以了. 有n个0..999之间的整数.. 如果有一个0那么就直接输出Y ...
- codeforces 577B. Modulo Sum 解题报告
题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...
随机推荐
- [转]XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks
感谢: XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks XNOR-Net ImageNet Cl ...
- Nexus 刷机
@echo offfastboot flash bootloader bootloader-hammerhead-hhz12k.imgfastboot flash radio radio-hammer ...
- RelativeLayout经常使用属性介绍
以下介绍一下RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHrizontal ...
- socket——本地服务器和android手机客户端通讯(防止中文乱码)
线上效果图: 服务端接收到的. 客户端接受到服务器返回的. server端代码直接运行在本地就可以了. 手机客户端运行在手机上就行. 先安装客户端,再启动server.然后再输入文字,点击发送. se ...
- asp.net页面按Enter键IE不提交表单
//当按下回车键时,让指定的按钮获取指定的文本框的事件 this.txtFNick.Attributes.Add("onkeydown", " ...
- 你所不了解的css选择器
我们目前接触到的选择器:.class #id div ...... 不了解的选择器:a>b a+b [a~=b] [a|=b]...... 一下说举5 6 7 8为css3中的定 ...
- sqlserver2012一直显示正在还原(Restoring)和从单用户转换成多用户模式(单用户连接中)
如果不需要还原,则使用: restore database test with recovery如果只需要还原,则使用: restore database test with norecovery U ...
- 在shell脚本中调用sqlplus
#!/bin/bash sqlplus dc_file_data_js/dc_file_data_js << EOF1 set linesize 500; set pagesize 100 ...
- 轻松实现HTML5时钟(分享下自己对canvas的理解,原来没你想像的那么难哦)
Hey,guys! 让我们一起用HTML5实现一下简易时钟吧! 接触canvas时, 我突然有一种非常熟悉的感觉------canvas的部分的功能其实和Photoshop中的 钢笔工具 是一样的.所 ...
- windows下配置Nginx+Mysql+Php7
环境:Windows10 mysql-5.6.24-win32解压缩版 nginx-1.8.0 php7 1.Mysql安装 下载压缩文件之后解压缩至相应目录(我的目录是G:\wnmp\m ...