URAL 2018 The Debut Album(dp)

题目链接

2018. The Debut Album

Time limit: 2.0 second
Memory limit: 64 MB
Pop-group “Pink elephant” entered on recording their debut album. In fact they have only two songs: “My love” and “I miss you”, but each of them has a large number of remixes.
The producer of the group said that the album should consist of n remixes. On second thoughts the musicians decided that the album will be of interest only if there are no more than a remixes on “My love” in a row and no more than b remixes on “I miss you” in a row. Otherwise, there is a risk that even the most devoted fans won’t listen to the disk up to the end.
How many different variants to record the album of interest from n remixes exist? A variant is a sequence of integers 1 and 2, where ones denote remixes on “My love” and twos denote remixes on “I miss you”. Two variants are considered different if for some i in one variant at i-th place stands one and in another variant at the same place stands two.

Input

The only line contains integers n, a, b (1 ≤ a, b ≤ 300; max( a, b) + 1 ≤ n ≤ 50 000).

Output

Output the number of different record variants modulo 10 9+7.

Sample

inputoutput
3 2 1
4

Hint

In the example there are the following record variants: 112, 121, 211, 212.

题意:已知 n,a,b。求对于长度为n只包含数字1,2的串,连续的1不超过a个,连续的2不超过b个的串有多少个?

题解:用dp[ i ][ 1,2 ][ k ] 表示前i个数字,第i个为1或2,以第i个结尾的连续的1和2的个数为k,该状态下满足条件的数的个数。转移很简单,详情见代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#define nn 51000
#define mod 1000000007
typedef long long LL;
using namespace std;
int n,a,b;
LL dp[2][3][330];
int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&a,&b)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        int t=0;
        dp[1][1][1]=dp[1][2][1]=1;
        LL ix,fc;
        for(i=2;i<=n;i++)
        {
            ix=fc=0;
            for(j=1;j<=a;j++)
            {
                ix=(ix+dp[1-t][1][j])%mod;
            }
            for(j=1;j<=b;j++)
                fc=(fc+dp[1-t][2][j])%mod;
            for(j=1;j<=a;j++)
            {
                if(j>1)
                    dp[t][1][j]=(dp[t][1][j]+dp[1-t][1][j-1])%mod;
                else
                    dp[t][1][j]=(dp[t][1][j]+fc)%mod;
            }
            for(j=1;j<=b;j++)
            {
                if(j>1)
                {
                    dp[t][2][j]=(dp[t][2][j]+dp[1-t][2][j-1])%mod;
                }
                else
                    dp[t][2][j]=(dp[t][2][j]+ix)%mod;
            }
            t=1-t;
            for(j=0;j<=max(a,b);j++)
                dp[t][1][j]=dp[t][2][j]=0;
        }
        LL ans=0;
        for(i=1;i<=a;i++)
            ans=(ans+dp[1-t][1][i])%mod;
        for(i=1;i<=b;i++)
            ans=(ans+dp[1-t][2][i])%mod;
        printf("%lld\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值