SDKD 2017 Spring Team Training B G题 -The Debut Album or URAL - 2018 (dp 三维)

题目描述

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  nab (1 ≤  ab ≤ 300;  maxab) + 1 ≤  n ≤ 50 000).
Output
Output the number of different record variants modulo 10  9+7.
Example
input output
3 2 1
4

Notes

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



题意: 连续个1 不超过a个 连续个2 不超过b个 问n个的话   有多少种


dp 推 状态方程 一直是弱项, 推了好久不出来,     用一个三维数组dp  

思路:

dp[i][j][k]  i代表构造第i个时 j代表是 1 还是2   k 是连续的长度

为了方便 用  i&1  操作 出现奇数或偶数  奇数1 偶数0 两种 分别代表 1 和2

所以状态转移方程为

**************************************************************************************************

 连续个1 不超过a  连续个2 不超过b  用0 代替1 用1 代替2
状态转移方程
i:1~n
     j:1~a// 0时
          dp[ i ][0][ j ] += dp[ i-1 ][0][ j-1 ] 当前与上一个都是0 则连续个数k的长度从j-1 变成j
          dp[ i ][1][1] += dp[ i-1 ][0][ j ] 当前与上一个不一样, 连续个数k重置 为1
     j:1~b// 1 时
          dp[ i ][1][ j ] += dp[ i-1 ][1][ j-1 ]  同理 
          dp[ i ][0][1 ] += dp[ i-1 ][1][ j ] 


**************************************************************************************************

数据量n: 5w  a,b -300 三维的话 应该开 5w*2*300  -  -  超内存了 ,直接爆了

所以这个地方又用到了滚动数组  滚啊滚就不会超了....

开个2*2*300的 就足够了


代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
/*
 连续个1 不超过a  连续个2 不超过b  用0 代替1 用1 代替2
状态转移方程
i:1~n
     j:1~a// 0时
          dp[ i ][0][ j ] += dp[ i-1 ][0][ j-1 ] 当前与上一个都是0 则连续个数k的长度从j-1 变成j
          dp[ i ][1][1] += dp[ i-1 ][0][ j ] 当前与上一个不一样, 连续个数k重置 为1
     j:1~b// 1 时
          dp[ i ][1][ j ] += dp[ i-1 ][1][ j-1 ]  同理 
          dp[ i ][0][1 ] += dp[ i-1 ][1][ j ]
*/
const int mod=1e9+7;
using namespace std;
int dp[2][2][310];
int main()
{
    int n,a,b;
    int i,j;
    while(cin>>n>>a>>b)
    {
        memset(dp,0,sizeof(dp));
        dp[0][0][0]=dp[0][1][0]=1;//初始化
        for(i=1;i<=n;i++)
        {
            int now=i&1;// 0/1
            int pre=(now+1)&1;
            memset(dp[now],0,sizeof(dp[now]));
            for(j=1;j<=a;j++)//1
            {
                dp[now][0][j]+=dp[pre][0][j-1];//当上一个现在一样的话  j为连续的
                dp[now][0][1]%=mod;
                dp[now][1][1]+=dp[pre][0][j];//不一样是 后缀长度就变成1 重新开始
                dp[now][1][1]%=mod;
            }
            for(j=1;j<=b;j++)//2
            {
                dp[now][1][j]+=dp[pre][1][j-1];
                dp[now][1][j]%=mod;
                dp[now][0][1]+=dp[pre][1][j];
                dp[now][0][1]%=mod;
            }
        }
        int ans=0;
        for(i=1;i<=a;i++)
        {
            ans+=dp[n&1][0][i];
            ans%=mod;
        }
        for(i=1;i<=b;i++)
        {
            ans+=dp[n&1][1][i];
            ans%=mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值