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/B
Description
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 Input
3 5
1 2 3
Sample Output
YES
HINT
题意
给你一堆数,然后问你是否有一些数加起来%m==0
题解:
当成背包dp做,空间为m,每一个物品的代价为a[i]就好了
注意滚动数组的时候,不要转移的时候被自己的状态转移了,注意一下就好了
代码:
//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000006
#define mod 1001
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=;
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 a[maxn];
bool dp[maxn][];
int main()
{
int n=read(),m=read();
for(int i=;i<=n;i++)
a[i]=read(),a[i]%=m;
int flag = ;
for(int i=;i<=n;i++)
{
dp[a[i]][-flag] = ;
for(int j=;j<m;j++)
{
if(dp[j][flag])
{
dp[(j+a[i])%m][-flag]=;
dp[j][-flag]=;
}
}
flag = -flag;
if(dp[][flag])
{
cout<<"YES"<<endl;
return ;
}
}
cout<<"NO"<<endl;
return ;
}
Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp的更多相关文章
- 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< ...
- 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 ...
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
D. Two Paths 题目连接: http://codeforces.com/contest/14/problem/D Description As you know, Bob's brother ...
- Codeforces Round #273 (Div. 2) D. Red-Green Towers 背包dp
D. Red-Green Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)
Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...
- 【DP】:CF #319 (Div. 2) B. Modulo Sum
[题目链接]:http://codeforces.com/contest/577/problem/B [相似题目]:http://swjtuoj.cn/problem/2383/ [题意]:给出n个数 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
随机推荐
- Codeforces Round #221 (Div. 2) Lever I.O.U.
这场cf 做的很差,,第一题犯了一个很低级的错误..把i写成了J.... 第二题 想的太复杂了...其实我们只需要 考虑每个人自己的负债情况就行了,就是假设每个人把别人 欠他的钱,拿过来还给别人..这 ...
- 8.21 usaco
summary:6 bzoj1692://后缀数组就行了O(nlogn)c[30]会RE...注意!!! #include<cstdio> #include<cstring> ...
- Nginx+Tomcat+Terracotta的Web服务器集群实做
1.准备工作两个Linux服务器,可以用VMware装一个,然后配置好再克隆一个,修改IP即可.Host1:192.168.0.79Host2:192.168.0.80先配置好jdk1.6.0和tom ...
- light工具
环境变量 C:\Users\zhangqm\AppData\Roaming\npm\; 安装 npm install -gd jresplus 不要用npm install -gd light pro ...
- win2003 多域名绑定一个ip
一个IP绑定多个域名 很多虚拟主机,只有一个IP,很多个域名都指向该IP,但都能访问自己域名所在 的网站的内容,这就是一个IP绑定多个域名的技术. 我们得先了解一个概念 什么是主机头所谓的主机头的叫法 ...
- Parallel for-each loops in .NET C# z
An IEnumerable object An Action of T which is used to process each item in the list List<string&g ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- 021QTP之焦点(多思考)
一.什么是焦点: 焦点说白了就是你打开某一个程序时默认的focuse 比如我们那QTP自带的windows下的示例程序来说,启动它后焦点自动落在了agent name文本框上 二.利用Tab键检查焦点 ...
- js基础第八天
返回前面起第一个字符位置 indexOf("sdfsdf");它是从左边索引为0开始数,而且只找第一个,然后返回该字符的位置.返回是个数值.如果找不到该字符,那么就会返回-1. 返 ...
- 第一天CSS实战培训及笔记及感想
首先,我很激动...... 3点了,凌晨3点了,我居然还没睡.总共不到3个小时的视频消化了6个小时,今天是培训班第一天,一下子就来高强度的讲课,整个上过基础班的都听得东倒西歪,更别说我这个没上基础班滴 ...