Codeforces Round #319 (Div. 2) B. Modulo Sum 抽屉原理+01背包
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 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
1 6
5
NO
4 6
3 1 1 3
YES
6 6
5 5 5 5 5 5
YES
题意:从n个数中选取任意个数(最少一个)使得对m取模为0;
思路:首先当n>m的时候,是必定的;
根据抽屉原理,前缀和必定有两个相等的数;sl==sr;
sr-sl=0;意思就是[l,r]的和%m==0;
n<m时,利用01背包,复杂度n*m;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e3+,M=1e6+,inf=1e9+;
int a[M];
int dp[N][N];
int max(int x,int y,int z)
{
return max(x,max(y,z));
}
int main()
{
int x,y,z,i,t;
memset(dp,,sizeof(dp));
scanf("%d%d",&x,&y);
for(i=;i<x;i++)
scanf("%d",&a[i]);
if(x>y)
{
printf("YES\n");
return ;
}
for(i=;i<x;i++)
dp[i][(a[i]%y)]=;
for(i=;i<x;i++)
{
for(t=;t<y;t++)
{
dp[i][t]=max(dp[i][t],dp[i-][t]);
dp[i][(t+a[i])%y]=max(dp[i-][t],dp[i][(t+a[i])%y]);
}
}
if(dp[x-][])
printf("YES\n");
else
printf("NO\n");
return ;
}
Codeforces Round #319 (Div. 2) B. Modulo Sum 抽屉原理+01背包的更多相关文章
- 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 DP
B. Modulo Sum ...
- 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 ...
- 【DP】:CF #319 (Div. 2) B. Modulo Sum
[题目链接]:http://codeforces.com/contest/577/problem/B [相似题目]:http://swjtuoj.cn/problem/2383/ [题意]:给出n个数 ...
- Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳
E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...
- Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)
传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...
随机推荐
- nginx集群配置
一.nginx集群目标 以nginx作为代理服务器,分别在两台部署web站点的机器上面轮询访问. 3台机器IP地址分别为: 1)192.168.189.133 (nginx代理服务器) 2)192 ...
- spring-boot集成redis
application.properties #redis 配置 # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redi ...
- linux 统计文件数量
查找当前目录下compose文件的数量 ls -lr | grep "compose" | wc -l
- 玩转Javascript 给JS写测试
给js写测试已经不是什么稀奇的事情了,最近项目里用了jasmine和JsTestDriver两种js测试框架.JsTestDriver易于与持续构建系统相集成并能够在多个浏览器上运行测试轻松实现TDD ...
- IOS使用Core-Plot画折线图
关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...
- 通过IP获取对应所在地的地址
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangshuxuncom/article/details/35988143 曾几何时 ...
- Linux(CentOS)系统下搭建svn服务器
由于GitHub的私有项目需要收费,gitlab对服务器的要求必须是4GB内存以上.对于一些个人的小型项目,想要免费的版本控制工具来管理自己的代码,又不想代码公开,无疑SVN是比较好的选择.windo ...
- git-flow工作流程
什么是 git-flow? 一旦安装安装 git-flow,你将会拥有一些扩展命令.这些命令会在一个预定义的顺序下自动执行多个操作.是的,这就是我们的工作流程! git-flow 并不是要替代 Git ...
- ZOJ2588:Burning Bridges(无向连通图求割边)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 吐下槽,不得不说ZOJ好坑,模版题做了一个多小时. 题意:* ...
- char *strstr(const char *str1, const char *str2);
[FROM MSDN && 百科] 原型:char *strstr(const char *str1, const char *str2); #include<string.h& ...