[ACM] ZOJ 3725 Painting Storages (DP计数+组合)
Painting Storages
Time Limit: 2 Seconds Memory Limit: 65536 KB
There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will
be painted with exactly one color.
Bob has a requirement: there are at least M continuous storages (e.g. "2,3,4" are 3 continuous storages) to be painted with red. How many ways can you paint all storages under
Bob's requirement?
Input
There are multiple test cases.
Each test case consists a single line with two integers: N and M (0<N, M<=100,000).
Process to the end of input.
Output
One line for each case. Output the number of ways module 1000000007.
Sample Input
4 3
Sample Output
3
Author: ZHAO, Kui
Contest: ZOJ Monthly, June 2013
解题思路:
这道题和省赛上的一道非常像啊。
。
假设曾经做过,省赛的时候也不会没思路。
。。
这道题单纯用组合是不行的。。。
题意为:用红蓝两种颜色给N个仓库(标号1—N)涂色。要求至少有M个连续的仓库涂成红色,问一共能够的涂色方案。
结果模1000000007
dp[i] 为 前i个仓库满足至少M个连续仓库为红色的方法数。
那么dp[M]肯定为1。 dp[0,1,2,3,......M-1] 均为0.
在求dp[i]的时候,有两种情况
一。前i-1个仓库涂色已经符合题意,那么第i个仓库涂什么颜色都能够,有 dp[i] = 2*dp[i-1] ;(有可能超出范围,别忘了mod)
二。加上第i个仓库涂为红色才构成M个连续仓库为红色。那么 区间 [i-m+1, i]。为红色,第i-m个仓库肯定是蓝色并且从1到i-m-1个仓库肯定是不符合题意的涂色。所以用1到i-m-1的仓库的全部涂色方法 2^(i-m-1) 减去符合题意的方法数dp[i-m-1] 。所以方法数为2^(i-m-1)
- dp[i-m-1]
代码:
#include <iostream>
#include <string.h>
using namespace std;
const int mod=1000000007;
const int maxn=100005;
int power[maxn+1];
int dp[maxn];//前i个仓库满足m个仓库为红色的方法数
int n,m; void getPower()//预处理出2的多少次方
{
power[0]=1;
for(int i=1;i<=maxn;i++)
power[i]=power[i-1]*2%mod;
} int main()
{
getPower();
while(cin>>n>>m)
{
memset(dp,0,sizeof(dp));
dp[m]=1;
for(int i=m+1;i<=n;i++)
dp[i]=(dp[i-1]*2%mod+power[i-m-1]-dp[i-m-1])%mod;
cout<<dp[n]<<endl;
}
return 0;
}
[ACM] ZOJ 3725 Painting Storages (DP计数+组合)的更多相关文章
- ZOJ 3725 Painting Storages(DP+排列组合)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5048 Sample Input 4 3 Sample Output ...
- ZOJ - 3725 Painting Storages
Description There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob ask ...
- zoj 3725 - Painting Storages(动归)
题目要求找到至少存在m个连续被染成红色的情况,相对应的,我们求至多有m-1个连续的被染成红色的情况数目,然后用总的数目将其减去是更容易的做法. 用dp来找满足条件的情况数目,, 状态:dp[i][0] ...
- ZOJ-3725 Painting Storages DP
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3725 n个点排列,给每个点着色,求其中至少有m个红色的点连续的数 ...
- 【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas
[题目]BZOJ 2111 [题意]求有多少1~n的排列,满足\(A_i>A_{\frac{i}{2}}\),输出对p取模的结果.\(n \leq 10^6,p \leq 10^9\),p是素数 ...
- 【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值
[题意]n位同学(其中一位是B神),m门必修课,每门必修课的分数是[1,Ui].B神碾压了k位同学(所有课分数<=B神),且第x门课有rx-1位同学的分数高于B神,求满足条件的分数情况数.当有一 ...
- 动态规划(DP计数):HDU 5116 Everlasting L
Matt loves letter L.A point set P is (a, b)-L if and only if there exists x, y satisfying:P = {(x, y ...
- 【POJ1952】逢低吸纳 dp+计数
题目大意:给定一个有 N 个数的序列,求其最长下降子序列的长度,并求出有多少种不同的最长下降子序列.(子序列各项数值相同视为同一种) update at 2019.4.3 题解:求最长下降子序列本身并 ...
- Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...
随机推荐
- Elasticsearchs的安装/laravel-scout和laravel-scout-elastic的安装
安装: https://github.com/medcl/elasticsearch-rtf 先下载包 下载解压后 cd elasticsearch-rtf-master ll bin/elastic ...
- 【转】4w+1h 教你如何做用户画像
记得14年开始做用户画像的时候,对于用户画像完全没有概念,以为是要画一幅幅图画,经过两年多的学习和理解,渐渐的总结出了一些方法和技巧,在这里就通过4个W英文字母开头和1个H英文字母开头的单词和大家分享 ...
- Oracle常用查询语句
"ORACLE数据字典视图的种类分别为:USER,ALL 和 DBA. USER_*:有关用户所拥有的对象信息,即用户自己创建的对象信息 ALL_*:有关用户可以访问的对象的信息,即用户自己 ...
- ntdsutil 清理弃用服务器-----待验证
例子是这样的: 一个森林里有两个树,mm.com和cc.com,分别有dc www.mm.com和vdc.cc.com, cc.com域的控制器崩溃,不想恢复,要彻底删除这个域,由于vdc.cc.co ...
- Java-获取Class对象的名称
package com.tj; public class MyClass2 { public static void main(String[] args) { Class cls = java.la ...
- 【01】markdown语法
[02]段落和换行 一个 Markdown 段落是由一个或多个连续的文本行组成,它的前后要有一个以上的空行(空行的定义是显示上看起来像是空的,便会被视为空行.比方说,若某一行只包含空格和制表符,则该行 ...
- x86保护模式-六 控制转移
控制转移可以分为两大类 :同一任务内的控制转移 和 任务间的控制转移(任务切换) 同一个任务内的控制转移可以分为段内转移 .特权级不变的段间转移和特权级改变的段间转移 段内转移与实模式相同 ...
- pytorch中torch.unsqueeze()函数与np.expand_dims()
numpy.expand_dims(a, axis) Expand the shape of an array. Insert a new axis that will appear at the a ...
- BZOJ 3240 [Noi2013]矩阵游戏 ——费马小定理 快速幂
发现是一个快速幂,然而过不去. 怎么办呢? 1.十进制快速幂,可以用来练习卡时. 2.费马小定理,如果需要乘方的地方,可以先%(p-1)再计算,其他地方需要%p,所以需要保存两个数. 然后就是分类讨论 ...
- BZOJ 1226 [SDOI2009]学校食堂Dining ——状压DP
看到B<=8,直接状态压缩即可. dp[i][j][k]表示当前相对位置是关于i的,并且i以前的已经就餐完毕,j表示i和之后的就餐情况,k表示上一个就餐的人的相对位置. 然后Dp即可 #incl ...