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 ...
随机推荐
- poj2月下旬题解
poj2388 水题 poj1273 最大流初步 poj2456 简单的二分答案 poj2309 论lowbit的重要性 poj1734 floyd求最小环 poj1001 细节题 poj2184 0 ...
- ZBreak
https://github.com/atskyline/ZBreak 最近用电脑用的多,总觉得有必要2个小时休息一会.就花了一点点时间写了这个小东西如果连续使用电脑超过2个小时会弹出一个窗口提示. ...
- jquery通过ajax获取数据(优化、封装)
下载页面查看: makeGrid.js ,column.js ,XiangMuGuanLi.aspx <div class="tb_container" id=&quo ...
- ODAC访问oracle时,提示:由于以前的函数求值超时,函数求值被禁用,必须继续执行才能正常返回
这是因为调试时会自动对Local/Watch等窗口里面(或鼠标停留所在)的变量求值,为了防止用户写的程序错误(比如死循环),系统有一个超时限制,如果某个属性的get中做了很复杂的操作(而不是简单地返回 ...
- 25、BroadCastRecevier
BroadCastRecevier 有两种注册方式 1. 清单文件里注册: 一旦应用程序被部署到手机, 广播接受者就会生效 2. 代码里面注册: 一旦代码所在的进程被杀死了, 广播接受者就失效了. 广 ...
- msp时钟设置程序
吐槽一下MSP430需要明白的东西: 在430中,一个时钟周期 = MCLK晶振的倒数.如果MCLK是8M,则一个时钟周期为1/8us: 一个机器周期 = 一个时钟周期,即430每个动作都能完成一个基 ...
- 【译】 AWK教程指南 7AWK应用实例
本节将示范一个统计上班到达时间及迟到次数的程序. 这程序每日被执行时将读入两个数据文件: * 员工当日到班时间的数据文件 ( 如下列的 arr.dat ) * 存放员工当月迟到累计次数的文件 当程序执 ...
- NOIP 2014 无线网络发射器选址
水题..直接暴力 #include<cstdio> #include<algorithm> using namespace std; const int inf = 0x3f3 ...
- 在win64位,python64位2.7版本中安装pyHook
今天看了一篇博文说的是利用pyhook监听键盘鼠标事件(感兴趣的可以看博客园中相关文章),文章中使用的pyHook模块的官方下载地址是:http://sourceforge.net/projects/ ...
- Spark系列(四)整体架构分析
架构流程图 说明 Driver端流程说明(Standalone模式) 使用spark-submit提交Spark应用程序Application. 通过反射的方式创建和构造一个DriverActor进 ...