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.
Sample Input
1
2 1 1 1 1 1
Sample Output
6
题目描述
有N1个香蕉,N2个桃子,N3个苹果,每一种水果不能连续饲喂的天数分别为D1,D2,D3.问喂完这N1+N2+N3个水果,一共有多少种饲喂方法.
解题思路
dp[kind][len][left0][left1][left2],left0,left1,left2分别表示每一种剩余的数量,然后上一次饲喂的种类是kind,且该种类水果已经连续喂了len天.
代码实现
#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
typedef long long ll;
const int mod = 1e9+7;
#define INF 0x3f3f3f3f
const int maxn = 57;
int sum;
int n[3],d[3];
int dp[3][maxn][maxn][maxn][maxn];
int dfs(int kind,int len,int left0,int left1 ,int left2)
{
if(len>d[kind]||left0<0||left1<0||left2<0) return 0;
if(dp[kind][len][left0][left1][left2]!=-1) return dp[kind][len][left0][left1][left2];
if(left0+left1+left2==0) return 1;
int res=0;
if(kind==0)
{
res=(res+dfs(kind,len+1,left0-1,left1,left2))%mod;
res=(res+dfs(1,1,left0,left1-1,left2))%mod;
res=(res+dfs(2,1,left0,left1,left2-1))%mod;
}
else if(kind==1)
{
res=(res+dfs(kind,len+1,left0,left1-1,left2))%mod;
res=(res+dfs(0,1,left0-1,left1,left2))%mod;
res=(res+dfs(2,1,left0,left1,left2-1))%mod;
}
else if(kind ==2 )
{
res=(res+dfs(kind,len+1,left0,left1,left2-1))%mod;
res=(res+dfs(0,1,left0-1,left1,left2))%mod;
res=(res+dfs(1,1,left0,left1-1,left2))%mod;
}
return dp[kind][len][left0][left1][left2]=res;
}
int main()
{
IO;
int T;
cin>>T;
while(T--)
{
memset(dp,-1,sizeof(dp));
cin>>n[0]>>n[1]>>n[2]>>d[0]>>d[1]>>d[2];
sum=n[0]+n[1]+n[2];
int ans=0;
ans=(ans+dfs(0,1,n[0]-1,n[1],n[2]))%mod;
ans=(ans+dfs(1,1,n[0],n[1]-1,n[2]))%mod;
ans=(ans+dfs(2,1,n[0],n[1],n[2]-1))%mod;
cout<<ans<<endl;
}
return 0;
}