Time Limit: 2000MS Memory limit: 131072K
题目描述
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.
输入
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).
输出
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.
示例输入
1
2 1 1 1 1 1
示例输出
6
提示
Answers are BPBA, BPAB, BABP, BAPB, PBAB, and ABPB(B-banana P-peach A-apple)
题意:给你三种水果的数量和每一个水果不能连续的数量,问有多少中组合方式。
思路:Dp[i][j][k][f]表示第一种水果剩余i个第二种水果剩余j个,第三种水果剩余k个,以f种水果结尾连续数量在规定范围内的组合种类,那么第一种水果为例,连续的为s个:Dp[i-s][j][k][0] += Dp[i][j][k][1]+Dp[i][j][2] ;
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL Mod = 1e9+7;
LL Dp[55][55][55][4];
int d[4];
int main()
{
int T;
int n1,n2,n3,d1,d2,d3;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d %d %d %d",&n1,&n2,&n3,&d[1],&d[2],&d[3]);
memset(Dp,0,sizeof(Dp));
for(int i = n1;i>=0;i--)
{
for(int j = n2;j>=0;j--)
{
for(int k = n3;k>=0;k--)
{
for(int s = 1;s<=min(i,d[1]);s++)
{
if(i == n1 && j == n2 && k == n3)
{
Dp[i-s][j][k][0] = (Dp[i-s][j][k][0]+1)%Mod;
}
else
{
Dp[i-s][j][k][0] = ((Dp[i-s][j][k][0]+Dp[i][j][k][1])%Mod+Dp[i][j][k][2])%Mod;
}
}
for(int s = 1;s<=min(j,d[2]);s++)
{
if(i == n1 && j == n2 && k == n3)
{
Dp[i][j-s][k][1] = (Dp[i][j-s][k][1]+1)%Mod;
}
else
{
Dp[i][j-s][k][1] = ((Dp[i][j-s][k][1]+Dp[i][j][k][0])%Mod+Dp[i][j][k][2])%Mod;
}
}
for(int s = 1;s<=min(k,d[3]);s++)
{
if(i == n1 && j == n2 && k == n3)
{
Dp[i][j][k-s][2] = (Dp[i][j][k-s][2]+1)%Mod;
}
else
{
Dp[i][j][k-s][2] = ((Dp[i][j][k-s][2]+Dp[i][j][k][0])%Mod+Dp[i][j][k][1])%Mod;
}
}
}
}
}
LL ans = 0;
ans = ((Dp[0][0][0][0]+Dp[0][0][0][1])%Mod+Dp[0][0][0][2])%Mod;
printf("%lld\n",ans);
}
return 0;
}