题目链接点击打开链接
Feed the monkey
2000 ms 131072 KiBAccepted/Submissions: 7/28 (25.00%)
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.
Sample
Input
Copy1
2 1 1 1 1 1
Output
Copy6
dp[maxn][maxn][maxn][4][maxn]
前面三维分别表示第一二三种水果还剩多少
四维代表上一次选择了哪种水果,五维代表连续选择了几次该种水果
AC代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#define LL long long
#define mod 1000000007
#define maxn 51
using namespace std;
int t;
int n1,n2,n3,d[3];
LL dp[maxn][maxn][maxn][3][maxn];
LL ans;
LL dfs(int x,int y,int z,int pos,int cnt)
{
if(x<0||y<0||z<0||cnt>d[pos])
return 0;
if(x==0&&y==0&&z==0)
return 1;
if(dp[x][y][z][pos][cnt]!=-1)
return dp[x][y][z][pos][cnt];
LL sum=0;
if(pos==0)
{
sum=(sum+dfs(x-1,y,z,0,cnt+1))%mod;
sum=(sum+dfs(x,y-1,z,1,1))%mod;
sum=(sum+dfs(x,y,z-1,2,1))%mod;
}
else if(pos==1)
{
sum=(sum+dfs(x-1,y,z,0,1))%mod;
sum=(sum+dfs(x,y-1,z,1,cnt+1))%mod;
sum=(sum+dfs(x,y,z-1,2,1))%mod;
}
else if(pos==2)
{
sum=sum+dfs(x-1,y,z,0,1)%mod;
sum=sum+dfs(x,y-1,z,1,1)%mod;
sum=sum+dfs(x,y,z-1,2,cnt+1)%mod;
}
dp[x][y][z][pos][cnt]=sum;
return sum;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d%d",&n1,&n2,&n3,&d[0],&d[1],&d[2]);
for(int i=0; i<=n1; i++)
{
for(int j=0; j<=n2; j++)
{
for(int k=0; k<=n3; k++)
{
for(int x=0; x<3; x++)
{
for(int y=1; y<=50; y++)
dp[i][j][k][x][y]=-1;
}
}
}
}
ans=0;
ans=(ans+dfs(n1-1,n2,n3,0,1))%mod;
ans=(ans+dfs(n1,n2-1,n3,1,1))%mod;
ans=(ans+dfs(n1,n2,n3-1,2,1))%mod;
printf("%lld\n",ans);
}
return 0;
}