Codeforces Round #319 (Div. 2)B. Modulo Sum DP
2 seconds
256 megabytes
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.
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
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.
题意:给你n,m,n个数,让你从中找出任意数的和mod M==0
题解:背包dp
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define maxn 1000005
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 hashs[maxn],a[maxn*];
bool dp[][maxn];
int main()
{ mem(hashs);
int flag=;
int n=read(),m=read();
FOR(i,,n)
{
scanf("%d",&a[i]);
a[i]=a[i]%m;
if(a[i]==)a[i]=m;
}dp[][]=true;
dp[][]=true;
for(int i=;i<=n;i++)
{
for(int j=m;j>=;j--)
{
if(dp[][j])
{
if(j+a[i]==m){
puts("YES");
return ;
}
dp[][(j+a[i])%m]=true;
}
}
memcpy(dp[],dp[],sizeof(dp[]));
}
cout<<"NO"<<endl;
return ;
}
代码君
Codeforces Round #319 (Div. 2)B. Modulo Sum DP的更多相关文章
- 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,鸽巢)
		
直接O(n*m)的dp也可以直接跑过. 因为上最多跑到m就终止了,因为前缀sum[i]取余数,i = 0,1,2,3...,m,有m+1个余数,m的余数只有m种必然有两个相同. #include< ...
 - Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)
		
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...
 - Codeforces Round 319 # div.1 & 2 解题报告
		
Div. 2 Multiplication Table (577A) 题意: 给定n行n列的方阵,第i行第j列的数就是i*j,问有多少个格子上的数恰为x. 1<=n<=10^5, 1< ...
 - Codeforces Round #319 (Div. 2)
		
水 A - Multiplication Table 不要想复杂,第一题就是纯暴力 代码: #include <cstdio> #include <algorithm> #in ...
 - Codeforces Round #302 (Div. 2).C. Writing Code (dp)
		
C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard inp ...
 - Codeforces Round #338 (Div. 2) C. Running Track dp
		
C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...
 - Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
		
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
 
随机推荐
- IDEA、Eclipse快捷键对比
			
IDEA.Eclipse快捷键对比 序号 功能 IDEA Eclipse 1 很多功能:导入包,处理异常,强转cast Alt+Enter 2 导入包,自动修正??? Ctrl+Enter 3 ...
 - Go:map
			
一.map的创建方式 func main() { // map创建方式1 // 声明后再make var stu1 map[int]string stu1 = make(map[int]string, ...
 - Auto-Encoders实战
			
目录 Outline Auto-Encoder 创建编解码器 训练 Outline Auto-Encoder Variational Auto-Encoders Auto-Encoder 创建编解码器 ...
 - 指定PING的网卡
			
struct ifreq ifr; // 绑定在eth0上 memset( &ifr, , sizeof( struct ifreq ) ); snprintf( ifr.ifr_name, ...
 - STM32——GPIO口的八种工作模式
			
GPIO的输入工作模式1——输入浮空模式: GPIO_Mode_IN_FLOATING =0x04 工作原理:配置完相应寄存器为此工作模式后,高低电平信号通过1处的IO口输入进去,由于寄存器配置了的缘 ...
 - 13-看图理解数据结构与算法系列(Trie树)
			
Trie树 Trie树,是一种搜索树,也称字典树或单词查找树,此外也称前缀树,因为某节点的后代存在共同的前缀.它的key都为字符串,能做到高效查询和插入,时间复杂度为O(k),k为字符串长度,缺点是如 ...
 - [bzoj1078][SCOI2008][斜堆] (贪心)
			
Description 斜堆(skew heap)是一种常用的数据结构.它也是二叉树,且满足与二叉堆相同的堆性质:每个非根结点的值都比它父亲大.因此在整棵斜堆中,根的值最小.但斜堆不必是平衡的,每个结 ...
 - POJ1222熄灯问题【位运算+枚举】
			
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14231 Accepted: 8 ...
 - Texture 纹理贴图
			
基础贴图Shader:只有纹理 1. 在属性中声明纹理贴图: _MainTex ("Texture", 2D) = "white" {} 2. 在Pass中声明 ...
 - EGit应用
			
[创建Dynamic Web Project项目] [创建仓库] 项目(鼠标右键) ==〉Team==〉Share Project..... ==〉选择Git 配置Repository的目录 创建完成 ...