您的位置:首页 > 其它

动态规划 URAL 2018

2017-05-02 21:55 567 查看


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,a,b,,,,,n代表总共有多少个音符,a代表第一个音符最多连续出现次数,b

表示第二个音符最多出现的次数,然后让你统计总共有多少种不同的组合。

分析:组合数?数据量太大了,其他的数学知识也没想起来,dp?宝宝也没退出来,之后看了看大神的博客,

用下面这种递推公式:

for(int i=1;i<=n;i++){

            for(int j=1;j<=min(i,a);j++){

                dp[i][0]=(dp[i][0]+dp[i-j][1])%mod;

            }

            for(int j=1;j<=min(i,b);j++){

                dp[i][1]=(dp[i][1]+dp[i-j][0])%mod;

            }

        }

我来解释一下,最外层的for循环就代表此时音符的长度,最大为n,里面的第一个for循环是在寻找填入0的时候的情况,最多也就是min(i,a)个,里面的就是那个加法,就是如果这一个为0了,那么他前面如果第min(i,a)个之后的一定是合法的,这种方法是把合法的求和,

第二个for循环就是差不多的意思了,最后 对0和1这两种情况求一下和 就好啦  

切记  要取余 要取余 要取余

ac代码啊:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define mt(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f

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)){
mt(dp,0);
dp[0][0]=dp[0][1]=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=min(i,a);j++){
dp[i][0]=(dp[i][0]+dp[i-j][1])%mod;
}
for(int j=1;j<=min(i,b);j++){
dp[i][1]=(dp[i][1]+dp[i-j][0])%mod;
}
}
printf("%d\n",(dp
[0]+dp
[1])%mod);

}

return 0;
}


还有一种方法,这里先推荐俩大神的博客 http://blog.csdn.net/qq_34374664/article/details/71023530 http://blog.csdn.net/u010770930/article/details/40149525
另一个递推公式为

状态转移方程:

i:1~n

     j:1~a

          dp[ i ][0][ j ] += dp[ i-1 ][0][ j-1 ]

          dp[ i ][1][1] += dp[ i-1 ][0][ j ]

     j:1~b

          dp[ i ][1][ j ] += dp[ i-1 ][1][ j-1 ]

          dp[ i ][0][1 ] += dp[ i-1 ][1][ j ]
具体见这个大神的博客(点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: