CF 577B Modulo Sum
题意:给一个长度为n的正整数序列,问能不能找到一个不连续的子序列的和可以被m整除。
解法:抽屉原理+dp。首先当m<n时一定是有答案的,因为根据抽屉原理,当得到这个序列的n个前缀和%m时,一定会出现两个相同的数,这两个前缀和相减得到的序列和一定可以被m整除。当n<=m时,dp一下就可以了,类似01背包。
其实可以直接dp,只要滚动数组+在找到答案时break就可以了,同样因为抽屉原理,当枚举到第m+1个物品的时候就一定会得到解,所以最后复杂度O(m^2)。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long using namespace std; int a[1005];
bool dp[1005][1005];
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m))
{
memset(dp, 0, sizeof dp);
if(n > m)
{
int x;
for(int i = 0; i < n; i++)//不读入会RE哟~别问我怎么知道的_(:з」∠)_
scanf("%d", &x);
puts("YES");
continue;
}
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
bool ans = 0;
for(int i = 1; i <= n && !ans; i++)
{
a[i] %= m;
dp[i][a[i]] = 1;//与01背包不同,背包内必须有物品,所以不可以直接从0状态转移
for(int j = 0; j < m && !ans; j++)
{
dp[i][j] |= dp[i - 1][j];//不选当前物品
dp[i][(j + a[i]) % m] |= dp[i - 1][j];//选当前物品
if(dp[i][0]) ans = 1;
}
}
if(ans) puts("YES");
else puts("NO");
}
return 0;
}
写的时候脑子里全是01背包……然而有一些差异……结果写的乱七八糟……
CF 577B Modulo Sum的更多相关文章
- CodeForce 577B Modulo Sum
		
You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choo ...
 - Codeforces 577B Modulo Sum
		
http://codeforces.com/problemset/problem/577/B 题意:有n个数,求有无一个子序列满足和是m的倍数 思路:用模下的背包做,发现n是十的六次方级别,但是有个神 ...
 - Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】
		
题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解 ...
 - codeforces 577B. Modulo Sum  解题报告
		
题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...
 - 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 ...
 
随机推荐
- Install wget in Mac OS X Without Homebrew or MacPorts
			
May 22, 2012 - 31 Comments The command line tool wget lets you retrieve a group of files from FTP an ...
 - POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
			
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
 - JS 变量或参数是否有值的判断
			
var node; …… 判断 node 是否有值,是否为 undefine,是否 null,直接使用两个!!,否定之否定: if (!!node){ .... }else{ .... } 这个条件判 ...
 - SDUT1466双向队列
			
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1466&cid=1182 题目描述 想想双向链表……双向队列的定义差不多,也就是说一个队列 ...
 - Ubuntu 启动栏添加eclipse图标
			
sudo gedit /usr/share/applications/eclipse.desktop [Desktop Entry] Name=Eclipse Comment=c project ma ...
 - mac下安装mysql 连接时候报错 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
			
http://segmentfault.com/q/1010000000094608 同样的问题,先在网上搜了下其他人解决这个问题的方法,是去修改mysql.cnf文件添加mysql.sock文件路径 ...
 - 套题T1
			
间隙妖怪(gap.cpp/c/pas) 题目描述: 八云紫是幻想乡的间隙妖怪.她喜欢和八云橙玩一个叫做翻转的游戏.具体规则如下,八云紫对一个长度为N字符串做M次翻转操作,每次操作给定一个X,八云紫将X ...
 - Project Euler 95:Amicable chains 亲和数链
			
Amicable chains The proper divisors of a number are all the divisors excluding the number itself. Fo ...
 - java io流缓冲理解
			
bufferedinputstream和bufferedoutputstream:这两个类是在inputstream和outputstream的基础上增加了一个buffer的缓冲区,从而使数据不直接写 ...
 - iOS使用Charts框架绘制—柱形图
			
首先看一下最终要实现的效果: 最终效果 一.初始化barChartView 绘制柱形图需要用到BarChartView这个类,下面是初始化代码: self.barChartView = [[BarCh ...