[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;
}
时间: 2024-09-29 02:45:02

[ACM] ZOJ 3725 Painting Storages (DP计数+组合)的相关文章

ZOJ - 3725 Painting Storages

Description 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 le

(DP) zoj 3725

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 e

[ACM] ZOJ 3329 One Person Game (概率DP,有环,巧妙转化)

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the

【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值

[题意]n位同学(其中一位是B神),m门必修课,每门必修课的分数是[1,Ui].B神碾压了k位同学(所有课分数<=B神),且第x门课有rx-1位同学的分数高于B神,求满足条件的分数情况数.当有一位同学的一门必修课分数不同时视为两种情况不同.n,m<=100,Ui<=10^9. [算法]计数DP+排列组合+拉格朗日插值 [题解]把分数作为状态不现实,只能逐门课考虑. 设$f[i][j]$表示前i门课,有j个同学被碾压的情况数,则有: $$f[i][j]=g(i)\cdot\sum_{k=j

HDU--5396(区间dp+排列组合)

做这道题的时候,想到会不会是dp,然后发现dp可做,但是一直被自己坑到死. 枚举最后合并的那个位置,然后对于加减号的,分成的前后两个部分都有不同的组合方法, (a1+a2........) +  (b1,b2.............)         对于每个a,被加b的个数的阶乘次 ,对于每个b,被加a的个数的阶乘次 减法同理 乘法特殊一点 (a1+a2........) *  (b1,b2.............)  乘法分配率,直接将两部分的总和相乘即可 想到这些还远远没有结束,因为最

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

ZOJ3380- Patchouli&#39;s Spell Cards(概率DP+计数)

Patchouli's Spell Cards Time Limit: 7 Seconds      Memory Limit: 65536 KB Patchouli Knowledge, the unmoving great library, is a magician who has settled down in the Scarlet Devil Mansion (紅魔館). Her specialty is elemental magic employing the seven ele

动态规划(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), (x + 1, y), . . . , (x + a, y), (x, y + 1), . . . , (x, y + b)}(a, b ≥ 1) A point set Q is good if and only if Q is an (a, b)-L set and gcd(a, b)

[ACM] POJ 3071 Football (概率DP)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2875   Accepted: 1462 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the