您的位置:首页 > 其它

URAL 2018 The Debut Album(DP)

2017-05-02 11:07 435 查看


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 109+7.

Sample

inputoutput
3 2 1

4

Notes

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

题目的大意是指,给出n长度的数列,其实1的连续个数不超过a,2的连续个数不超过b

这种水dp就不三步走战略了,直接上,遍历连着的a或连着的b就好了。

一道比较水的基础DP题目。

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9+7;
int n,a, b, dp[3][50005];///dp[i][j]是指长度为j,以i结尾的序列的种数,它是由后面小于等于a/b长度均为1/2的序列的种数累加而成的
int main()
{
scanf("%d%d%d",&n,&a,&b);
dp[1][0] = dp[2][0] = 1;
for(int i = 1; i <= n; ++ i)
{
for(int j = 1; j <= a&&i>=j; ++ j)
dp[1][i] = (dp[1][i] + dp[2][i-j]) % MOD;///i-j==0意味着最后的a个都是1,也是合法的序列
for(int j = 1; j <= b&&i>=j; ++ j)
dp[2][i] = (dp[2][i] + dp[1][i-j]) % MOD;
}
printf("%d\n",(dp[1]
+ dp[2]
) % MOD) ;
return 0;
}

当时做这道题的时候我在A其他题,所以具体代码我也就没看,完了之后发现代码简直是一坨Shit,完全不符合W神的称号啊,默默地讽刺他一波

,反正估计以后他也不会在看这种水题的代码了,呃呃呃。。。其实整体思路是一样的,不过他这种分情况了,如果有10个字母,这种思路怎么敲。。ORZ。。

#include<bits/stdc++.h>
using namespace std;

#define mod 1000000007
int dp[2][305][2];
int main()
{
memset(dp, 0, sizeof(dp));
int n,a,b;
dp[0][1][0] = dp[1][1][0] = 1;
scanf("%d%d%d",&n,&a,&b);
int kk = 1;
// int ml = max(a,b);
for(int l = 2; l <= n; l++){

int ml = (l-1)<a?(l-1):a;
dp[1][1][kk] = 0;
for(int i = 1; i <= ml; i++ ){
dp[1][1][kk] = (dp[0][i][kk^1] + dp[1][1][kk])%mod;
}

ml = (l-1)<(b-1)?(l-1):(b-1);
for(int i = 1; i <= ml; i++){
if(i != 0) dp[1][i+1][kk] = 0;
dp[1][i + 1][kk] = (dp[1][i + 1][kk] + dp[1][i][kk^1])%mod;
}

ml = (l-1)<b?(l-1):b;
dp[0][1][kk] = 0;
for(int i =1 ; i <= ml ;i++){
dp[0][1][kk] =(dp[0][1][kk] + dp[1][i][kk^1])%mod;
}

ml = (l-1) <(a-1)?(l-1):(a-1);
for(int i= 1; i <= ml; i++){
if(i != 0) dp[0][i+1][kk] = 0;
dp[0][i+1][kk] = (dp[0][i+1][kk] + dp[0][i][kk^1])%mod;
}
kk ^= 1;
}
int ans = 0;
int mx = max(a,b);
for(int i = 1; i <= mx; i++){
if(i <= a) ans =(ans + dp[0][i][kk^1])%mod;
if(i <= b) ans =(ans + dp[1][i][kk^1])%mod;
}
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: