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<< ...
随机推荐
- 利用Web服务器网络打洞
好了有些标题党了.这里想说的是:某些网络,除了http 80服务,其它端口的服务都被限制了,这个时候可以用http web服务器来进行代理转发. 以Apache为例,支持ssh登录到其它服务器的配置如 ...
- PMP-合同类型
1.Fixed-price or lump-sum contracts(固定总价或总包合同)简称FP 这是买方(甲方)最能受益的合同类型.不管乙方开销多大,甲方付的钱是固定不变的. 2.Time an ...
- ES VS Hbase
http://db-engines.com/en/system/Elasticsearch%3BHBase
- JSBinding + SharpKit / 生成JavaScript绑定
将 UnityEngine 的代码导出到 JavaScript.就可以在 JavaScript 中使用 Unity 的功能. 如何导出? 将需要导出的类添加到 JSBindingSetting.cla ...
- 通过js对表单对象的便捷获取
<form name="a"> <input name="s" value="33"/> </form> ...
- VC++ 0xC0000005: Access violation.
public: COptionDlg(CWnd* pParent = NULL); // 标准构造函数 virtual ~COptionDlg(); TCONFIG m_tCfg; // 对话框数据 ...
- 经典ASP.NET MVC3.0入门详解
http://blog.csdn.net/csh624366188/article/details/7064269 :由于本文原在word文档里编写,写本文章时运用了大量截图,直接复制到博客里,没有显 ...
- jquery 使用attr() 函数对复选框无效的原因,javascript那些事儿——properties和attributes
复选框是网站开发的时候经常用到的网页标签之一,常见的在页面上对复选框的操作包括取值和修改复选框的状态.在jquery中,常见的操作标签的值得函数为attr,然而在操作复选框的时候,通常采用的却是pro ...
- cacti
http://www.cacti.net/downloads/docs/html/index.html Cacti脚本及模板论坛:http://forums.cacti.net/forum-12.ht ...
- MySQL查询本周、上周、本月、上个月份数据的sql代码
查询当前这周的数据 SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) ...