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 ...
随机推荐
- Publish over SSH插件安装
1 Publish over SSH插件安装 打开Jenkins的“系统管理>管理插件”,选择“可选插件”,在输入框中输入“Publish over SSH”进行搜索,如果搜索不到可以在“已安装 ...
- Java开发环境的搭建(jdk,eclipse)
一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可. http://www.orac ...
- 170803、springboot jar包启动提示没有主清单属性
问题:SpringBoot打包成jar后运行提示没有主清单属性 解决: 补全maven中的bulid信息 <plugin> <groupId>org.springframewo ...
- Git 进阶操作(一)
1. 获取提交信息(commit) git show 1c002d(哈希值的前几位): 获取提交的信息; git show HEAD^: 显示HEAD的上级(parent)提交的信息; git sho ...
- SprinBoot CLI 安装(Mac版)
1. 安装 HomeBrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/instal ...
- Nginx高级玩法
1. Nginx获取自定义消息头 .nginx是支持读取非nginx标准的用户自定义header的,但是需要在http或者server下开启header的下划线支持: underscores_in_h ...
- Magento 2 初探
进入公司有一小段时间了,虽然自己之前一直从事前端工作,但是基本工作就是做一些国内电商网站的前端工作.在刚进入这家公司时,自己对 magento2 一无所知,尽管上班前看过老大发给我的一些文档资料,但是 ...
- Python知识总汇
一.python基础 python基础 python编码问题 逻辑运算 二.python数据类型 二.python数据类型 三.IO(文件处理) 三.IO(文件处理) 四.函数 函数基础 名称空间与作 ...
- CSS之Flex 布局:语法篇
网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如 ...
- 阿里云centos7.2 安装mysql 6.5
[root@111 ~]# uname -aLinux bxhs 3.10.0-327.22.2.el7.x86_64 #1 SMP Thu Jun 23 17:05:11 UTC 2016 x86_ ...