动态规划 URAL 2018
2017-05-02 21:55
567 查看
2018. The Debut Album
Time limit: 2.0 secondMemory 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
input | output |
---|---|
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 ]
具体见这个大神的博客(点击打开链接)
相关文章推荐
- URAL 1210 Kind Spirits (DP动态规划)
- 【jzoj5219】【GDOI2018模拟7.10】【B】【动态规划】
- BZOJ5190 Usaco2018 Jan Stamp Painting(动态规划)
- POJ2018 Best Cow Fences——二分答案+贪心(动态规划)求最大子段和——pku2018
- hdu 2018 母牛的故事(动态规划)
- ural 1073. Square Country 动态规划
- URAL 2018 The Debut Album(dp)
- poj 动态规划DP - 2018 Best Cow Fences
- URAL - 2018 The Debut Album(状态dp 记忆化搜索)
- URAL 1009|URAL 1012|URAL 1013|K-based Numbers|高精度|动态规划
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
- URAL 1119 Metro (动态规划)
- URAL 1017|Staircases|动态规划
- ural1017 Staircases (动态规划)
- ☆URAL 2018 The Debut Album (dp 两种写法)
- ural 2018 The Debut Album(dp¥)
- 【GDOI2018模拟7.10】B 动态规划
- 【GDOI2018模拟7.10】C 动态规划
- 【jzoj5237】【GDOI2018模拟8.7】【最长公共子序列 】【动态规划】
- Ural 2018 The Debut Album