1. 程式人生 > >[ACM] ZOJ 3725 Painting Storages (DP計數+組合)

[ACM] ZOJ 3725 Painting Storages (DP計數+組合)

for each data data- 滿足 single mil trac pop namespace

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計數+組合)