☆URAL 2018 The Debut Album (dp 两种写法)

来源:互联网 发布:手机模板软件 编辑:程序博客网 时间:2024/06/05 10:42

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 nab (1 ≤ ab ≤ 300; max(a,b) + 1 ≤ n ≤ 50 000).

Output

Output the number of different record variants modulo 109+7.

Sample

inputoutput
3 2 1
4

Notes

In the example there are the following record variants: 112, 121, 211, 212.
Problem Author: Olga Soboleva (prepared by Alex Samsonov)

Problem Source: NEERC 2014, Eastern subregional contest

题意:给你一个n,代表有n个数字,每个数字要么1要么2,给你a,b代表1不能超过a个,2不能超过b个问你有几种方法,对1e9

+7 mod;

思路:我只会硬dp,三维数组dp[i][j][k]代表第i个数是j并且跟前面组成k个j, 那就很简单了,对于每个i有1,2两种可能,当时1的时候,如果前面也是1连续的最多有b个,转移方程就是 for:1-a, dp[i][1][k] += dp[i-1][1][k-1],前面一个字符必须也是1,长度有k-1个(因为他们一共组成k个),如果这个数字是1,前面是0,那么没有任何要求,可以有1-b个0,转移就很简单了,看代码,因为会爆内存,滚动一下i

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int Mod = 1e9 + 7;const int maxn = 5e4 +5 ;const int maxm = 305;int dp[2][2][maxm];int main(){    int n, a, b;    while(~scanf("%d%d%d", &n, &a, &b))    {        int p = 0;        dp[0][0][1] = 1;        dp[0][1][1] = 1;        for(int i = 2; i <= n; i++)        {            p ^= 1; //滚动数组            memset(dp[p], 0, sizeof(dp[p])); //这里每次都要清空一下,因为这个代表的是长度。。            for(int j = 1; j <= a; j++)  //枚举连续0的长度            {                dp[p][0][j] = (dp[p][0][j] + dp[p^1][0][j-1])%Mod; //如果前面也是0,那么久必须是0,长度j-1                dp[p][1][1] = (dp[p][1][1] + dp[p^1][0][j])%Mod; //这一位如果是1.前面是0,那么有几个0都行            }            for(int j = 1; j <= b; j++)            {                dp[p][1][j] = (dp[p][1][j] + dp[p^1][1][j-1])%Mod;                dp[p][0][1] = (dp[p][0][1] + dp[p^1][1][j])%Mod;            }        }        int ans = 0;        for(int i = 1; i <= a; i++)            ans = (ans + dp[p][0][i])%Mod;        for(int i = 1; i <= b; i++)            ans = (ans + dp[p][1][i])%Mod;        cout << ans <<endl;    }}

第二种思路佳爷跟潘老师想出来的,orz orz orz

其实就是枚举当前有几个连续的,比如第i位是1,他可以有j = 1-a个连续的1, 那么i-j就一定是2了。。只需要加上第i-j位是2的种类数就行。。

代码:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int maxn = 5e4 + 5;const int Mod = 1e9 + 7;int dp[maxn][2];int main(){    int n, a, b;    while(~scanf("%d%d%d", &n, &a, &b))    {        memset(dp, 0, sizeof(dp));        dp[0][0] = dp[0][1] = 1;        for(int i = 1; i <= n; i++)        {            for(int k = 1; k <= min(i, a); k++)            {                dp[i][0] = (dp[i][0]+dp[i-k][1])%Mod;            }            for(int k = 1; k <= min(i, b); k++)            {                dp[i][1] = (dp[i][1]+dp[i-k][0])%Mod;            }        }        printf("%d\n", (dp[n][0]+dp[n][1])%Mod);    }    return 0;}


2 0
原创粉丝点击