CF577B Modulo Sum 好题
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
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 by6, 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.
题意:
给出一个数列,和一个数m,问能不能从这个数列中选出若干个数,使得这些数的和可以整除m
整除m,也就是%m==0
其实是个很水的01背包,每个数取和不取
dp[i][j]表示选择到第i个数,和模m==j的情况有没有(有1,没有0)
但是我们会发现n很大,m很小
根据抽屉原理,当n>=m时,一定能
当n<m时,此时的数据大小<=1000,这个时候的dp,复杂度为n*m<m*m,可以过了
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int maxm=1e3+;
const int maxn=1e6+; int dp[maxm][maxm];
int a[maxn]; int main()
{
int n,m; scanf("%d %d",&n,&m);
for(int i=;i<=n;++i){
scanf("%d",&a[i]);
a[i]%=m;
}
if(n>=m){
printf("YES\n");
return ;
} memset(dp,,sizeof dp); for(int i=;i<=n;i++){
dp[i][a[i]]=;
for(int j=;j<m;j++){
if(dp[i-][j]){
dp[i][j]=true;
dp[i][(j+a[i])%m]=true;
}
}
} if(dp[n][]>)
printf("YES\n");
else
printf("NO\n"); return ;
}
CF577B Modulo Sum 好题的更多相关文章
- codeforces 577B B. Modulo Sum(水题)
题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)
B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 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/ ...
- Modulo Sum(背包 + STL)
Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 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 577B Modulo Sum
http://codeforces.com/problemset/problem/577/B 题意:有n个数,求有无一个子序列满足和是m的倍数 思路:用模下的背包做,发现n是十的六次方级别,但是有个神 ...
- 【CodeForces 577B】Modulo Sum
题 题意 给你n(1 ≤ n ≤ 106)个数a1..an(0 ≤ ai ≤ 109),再给你m( 2 ≤ m ≤ 103)如果n个数的子集的和可以被m整除,则输出YES,否则NO. 分析 分两种情况 ...
- SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
随机推荐
- python中使用zip函数出现<zip object at 0x02A9E418>
在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...
- Java 控制反转和依赖注入模式【翻译】【整理】
Inversion of Control Containers and the Dependency Injection pattern --Martin Fowler 本文内容 Component ...
- php get set方法深入理解
在类当中,设计通用的set和get方法,可以简化对属性的读写,这种方法不同于针对于独立的属性的普通的get和set方法,后者针对每个属性,都必须提供一对方法,前者针对所有属性,因此,可以看作是批量定义 ...
- (转) SLAM系统的研究点介绍 与 Kinect视觉SLAM技术介绍
首页 视界智尚 算法技术 每日技术 来打我呀 注册 SLAM系统的研究点介绍 本文主要谈谈SLAM中的各个研究点,为研究生们(应该是博客的多数读者吧)作一个提纲挈领的摘要.然后,我 ...
- javaweb在线预览
需要工具: 1.openoffice 将word.excel.ppt.txt等文件转换为pdf文件 2.SWFTool 将pdf转换为swf文件 3.flexPaper是一个开源轻量级的在浏览器上显示 ...
- java 将长度很长的字符串(巨大字符串超过4000字节)插入oracle的clob字段时会报错的解决方案
直接很长的字符串插入到clob字段中会报字符过长的异常,相信大家都会碰到这种情况 String sql = "insert into table(request_id,table_name, ...
- Sublime Text 下配置python
Sublime Text 2作为一款轻量级的编辑器,特点鲜明,方便使用,而且支持多语言. 一.control+B方式 1. 在工具栏点击Preferences,打开Browse Packages.在 ...
- Windows帐户类型
摘自:http://blog.csdn.net/shineorrain/article/details/18181707 LocalSystem 账户 LocalSystem是预设的拥有本机所有 ...
- OpenJudge计算概论-计算三角形面积【海伦公式】
/*============================================== 计算三角形面积 总时间限制: 1000ms 内存限制: 65536kB 描述 平面上有一个三角形,它的 ...
- php download断点
FileDownload.class.php <?php /** php下载类,支持断点续传 * Date: 2013-06-30 * Author: fdipzone * Ve ...