简单动态规划两题(思想一样)

来源:互联网 发布:足球首发阵容软件 编辑:程序博客网 时间:2024/06/06 14:16

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] 

代码:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string.h>  
  3. using namespace std;  
  4. const int mod=1000000007;  
  5. const int maxn=100005;  
  6. int power[maxn+1];  
  7. int dp[maxn];//前i个仓库满足m个仓库为红色的方法数  
  8. int n,m;  
  9.   
  10. void getPower()//预处理出2的多少次方  
  11. {  
  12.     power[0]=1;  
  13.     for(int i=1;i<=maxn;i++)  
  14.         power[i]=power[i-1]*2%mod;  
  15. }  
  16.   
  17.   
  18. int main()  
  19. {  
  20.     getPower();  
  21.     while(cin>>n>>m)  
  22.     {  
  23.         memset(dp,0,sizeof(dp));  
  24.         dp[m]=1;  
  25.         for(int i=m+1;i<=n;i++)  
  26.             dp[i]=(dp[i-1]*2%mod+power[i-m-1]-dp[i-m-1])%mod;  
  27.         cout<<dp[n]<<endl;  
  28.     }  
  29.     return 0;  
  30. }  
http://4.gdutcode.sinaapp.com/problem.php?cid=1021&pid=0

gdut校赛  01串也疯狂之光棍也有伴

Description

话说春节那天,小明和晓明在实验室刷题。刷着刷着小明觉得累了,就邀请晓明一起看春晚。晓明觉得小明很无聊,不想理小明,但是小明很会磨嘴皮子,晓明耐不住小明的胡嘴蛮缠,于是和小明一起看起春晚来。

小明顿时觉得倍儿爽啊! 可是一看,“wocao”,“最炫小苹果”,小明顿时觉得很伤心。 “连小苹果都有伴了。。。呜呜。。。。” 晓明看到小明哭了,就想安慰他,可是怎么安慰呢!  
晓明陷入了沉思,忽然,晓明灵光一闪,想借一下出题名义,让小明开心起来。于是晓明对小明说,既然小苹果都有伴了,那我们两光棍离脱单也不远了吧! 。。。。噼噼啪啦,晓明对小明说不然我们也来让光棍有个伴吧! 正好,正值我们学校的校赛,我们就以光棍为名,来出一道题。小明听到要出题,立马起了劲。。。他们认为“11”是光棍成双成对的标志,于是, 小明和晓明想问下你们,对于一个长度为n的01串,到底有多少串是含有“11”子串的呢? 。。。聪明的你,相信你已想到怎么AC了。 
例如长度为2的有“11”一个符合条件的01串;  
        长度为3的有“111”,“110”,“011”三个符合条件的串;

        长度为4的有“1111”,“1101”,“1100”,“0011”,“1011”,“0111”,“0110”,“1110”八个符合条件的串。

Input

 有T组数据输入。(T<=1000); 
 每组数据只有一行,一个正整数n(1<=n<=10^6)

Output

对于每组数据输出一行结果,对1000000007取模。

Sample Input

3145

Sample Output

0819

HINT


思想和上面的一题一样,dp[i]代表长度为i的串符合题意的有多少种方法,那么dp[0]=0,dp[1]=0,dp[2]=1.

dp[i]有两种情况,1是前面长度i-1的串已经符合题意,那么第i个字符是0和1都可以,有两种选择,2*dp[i-1]

                                2是前面长度i-1的串没有符合题意,本为必须是1才符合题意,那么前一位也必须是1,因为要求前i位符合题意的情况有多少种,这样才符合题意,也就是第i-1位为1,第i位也为1,那么第i-2位必须为0,长度为i-2-1的串肯定是不符合题意的,求得方法就是长度为i-2-1所有情况减去符合题意的情况,即 2^(i-2-1)-dp[i-2-1].

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <stdlib.h>#include <cmath>#include <iomanip>#include <vector>#include <set>#include <map>#include <stack>#include <queue>#include <cctype>#define rd(x) scanf("%d",&x)#define rd2(x,y)  scanf("%d%d",&x,&y)#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)using namespace std;typedef long long ll;const ll mod=1000000007;const int maxn=1000010;ll dp[maxn];int n;ll two[maxn];void pre(){    two[0]=1;    for(int i=1;i<maxn;i++)    {        two[i]=2*two[i-1];        if(two[i]>=mod)            two[i]%=mod;    }    dp[0]=0;dp[1]=0;dp[2]=1;dp[3]=3;    for(int i=4;i<maxn;i++)    {        dp[i]=(2*dp[i-1]%mod+two[i-2-1]-dp[i-2-1])%mod;        while(dp[i]<0)//注意加上这一句话!!!            dp[i]+=mod;    }}int main(){    pre();    int t;    rd(t);    while(t--)    {        rd(n);        cout<<dp[n]<<endl;    }    return 0;}




0 0
原创粉丝点击