Feed the monkey
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
题意:你有三种水果各有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;
}