sdut oj 3665 Feed the monkey(dp)
2016-10-29 10:34
260 查看
Feed the monkey
[align=center]Time Limit: 2000MS Memory Limit: 131072KB [/align]Problem Description
Alice has a monkey, she must feed fruit to the monkey every day.She has three kinds of fruits, bananas,peaches and apples. Every day, she chooses one in three, and pick one of this to feed the monkey.
But the monkey is picky, it doesn’t want bananas for more than D1 consecutive days, peaches for more than D2
consecutive days, or apples for more than D3 consecutive days. Now Alice has N1 bananas, N2 peaches and N3
apples, please help her calculate the number of schemes to feed the monkey.
Input
Multiple test cases. The first line contains an integer T (T<=20), indicating the number of test case.Each test case is a line containing 6 integers N1, N2, N3, D1, D2, D3 (N1, N2, N3, D1, D2, D3<=50).
Output
One line per case. The number of schemes to feed the monkey during (N1+N2+N3) days.The answer is too large so you should mod 1000000007.
Example Input
12 1 1 1 1 1
Example Output
6
Hint
Answers are BPBA, BPAB, BABP, BAPB, PBAB, and ABPB(B-banana P-peach A-apple)Author
“浪潮杯”山东省第七届ACM大学生程序设计竞赛题意:你有三种水果各有a,b,c个,每天喂一个水果给猴子,猴子很有个性,它吃第一种水果的连续天数不超过d天,第二种水果e天,第三种水果f天。问有多少种喂法。
思路:但凡是问有多少种方法的一般都是dp,很显然这里有两个限制条件:
1.每个水果的个数固定。
2.水果的连续天数有限定。
第一个限制条件可以给数组开三维,每种水果占一维,这样第一个问题就解决了。
但是第二个该如何解决???
做的时候,是通过枚举每一天吃的水果来做,结果这样超复杂,根本做不动。。
聪明的我立刻想到
只能看题解了orz
题解就很牛逼了,对满足条件的每一种水果的连续天数进行枚举,完全摒弃了天数这种问题。
代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn=100; typedef long long ll; const int mod=1e9+7; ll dp[maxn][maxn][maxn][5]; int main() { int t; scanf("%d",&t); while(t--) { int n[4],d[4]; scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&d[1],&d[2],&d[3]); memset(dp,0,sizeof(dp)); for(int i=n[1];i>=0;i--) for(int j=n[2];j>=0;j--) for(int k=n[3];k>=0;k--) { for(int z=1;z<=min(i,d[1]);z++) { if(i==n[1]&&j==n[2]&&k==n[3]) { dp[i-z][j][k][0]+=1; continue; } dp[i-z][j][k][0]+=dp[i][j][k][1]+dp[i][j][k][2]; dp[i-z][j][k][0]%=mod; } for(int z=1;z<=min(j,d[2]);z++) { if(i==n[1]&&j==n[2]&&k==n[3]) { dp[i][j-z][k][1]+=1; continue; } dp[i][j-z][k][1]+=dp[i][j][k][0]+dp[i][j][k][2]; dp[i][j-z][k][1]%=mod; } for(int z=1;z<=min(k,d[3]);z++) { if(i==n[1]&&j==n[2]&&k==n[3]) { dp[i][j][k-z][2]+=1; continue; } dp[i][j][k-z][2]+=dp[i][j][k][0]+dp[i][j][k][1]; dp[i][j][k-z][2]%=mod; } } ll ans=0; for(int i=0;i<3;i++) ans+=dp[0][0][0][i]; ans%=mod; printf("%lld\n",ans); } return 0; }
相关文章推荐
- 山东省第7届ACM省赛 F题 Feed the monkey dp
- 第七届山东省赛 Feed the monkey(dp)
- SDUT 3565 Feed the monkey【Dp】
- SDUT 3565 Feed the monkey && sdut 2879 Colorful Cupcakes (dp)
- 山东省第七届 ACM 省赛 Feed the monkey (dp)
- sdut3565——Feed the monkey(记忆化DP)
- 第七届 山东省ACM Feed the monkey(记忆化搜索 OR DP )
- SDUT 3565 Feed the monkey(DP)
- sdut 3565 Feed the monkey dp
- 第7届山东省赛----sdut 3565 Feed the monkey
- 文章标题 CSU 1963: Feed the rabbit (斜率DP优化)
- 第七届ACM山东省赛-F Feed the monkey
- CSU-1963 Feed the rabbit(斜率优化dp)
- CSU 1963 Feed the rabbit(斜率优化dp)
- CodeForces 311B/CSU 1963 Cats Transport/Feed The Rabbit(斜率优化dp)
- 山东省第七届ACM省赛 Feed the monkey
- 山东省第七届ACM F Feed the monkey (记忆搜索)
- HDU 4126 Genghis Khan the Conqueror MST+树形dp
- hdu 1028 Ignatius and the Princess III(dp||母函数)
- poj 2151 Check the difficulty of problems (概率DP)