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
1 2 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
题目大意:
一共有三种食物,每一种食物的数量已知,每天吃任意一个食物,每一种的食物不能连续吃D1.D2.D3天,问可行的方案数有多少。
思路:
1、统计方案数,考虑dp.设定Dp【i】【j】【k】【l】【3】:
①Dp【i】【j】【k】【l】【0】:表示到第i天,第一种食物剩余j个,第二种食物剩余k个,第三种食物剩余l个,并且这天是吃了第一种食物的方案数。
②Dp【i】【j】【k】【l】【1】:表示到第i天,第一种食物剩余j个,第二种食物剩余k个,第三种食物剩余l个,并且这天是吃了第二种食物的方案数。
③Dp【i】【j】【k】【l】【2】:表示到第i天,第一种食物剩余j个,第二种食物剩余k个,第三种食物剩余l个,并且这天是吃了第三种食物的方案数。
2、那么有状态转移方程:
①Dp【i】【a】【b】【c】【0】+=dp【j】【a+cnt】【b】【c】【1】这里j<i,并且两个天数差小于等于D1.
①Dp【i】【a】【b】【c】【0】+=dp【j】【a+cnt】【b】【c】【2】这里j<i,并且两个天数差小于等于D1.
②Dp【i】【a】【b】【c】【1】+=dp【j】【a】【b+cnt】【c】【0】这里j<i,并且两个天数差小于等于D2.
②Dp【i】【a】【b】【c】【1】+=dp【j】【a】【b+cnt】【c】【2】这里j<i,并且两个天数差小于等于D2.
③Dp【i】【a】【b】【c】【2】+=dp【j】【a】【b】【c+cnt】【1】这里j<i,并且两个天数差小于等于D3.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll ;
const ll MOD = 1e9+7;
ll dp[165][52][52][52][3];
int main(){
ll t;
scanf("%lld",&t);
while(t--)
{
ll n1,n2,n3,d1,d2,d3;
scanf("%lld%lld%lld%lld%lld%lld",&n1,&n2,&n3,&d1,&d2,&d3);
memset(dp,0,sizeof(dp));
for(ll i=1;i<=n1+n2+n3;i++)
{
ll cnt=0;
for(ll j=i-1;j>=max(i-d1,0ll);j--)
{
cnt++;
for(ll a=0;a<=n1;a++)
{
for(ll b=0;b<=n2;b++)
{
ll c=n1+n2+n3-i-a-b;
if(c<=n3&&c>=0)
{
if(a+cnt>n1)continue;
if(j==0&&dp[i][a][b][c][0]==0)dp[i][a][b][c][0]+=1;
if(a+cnt<=n1)dp[i][a][b][c][0]=(dp[i][a][b][c][0]+dp[j][a+cnt][b][c][1])%MOD;
if(a+cnt<=n1)dp[i][a][b][c][0]=(dp[i][a][b][c][0]+dp[j][a+cnt][b][c][2])%MOD;
}
}
}
}
cnt=0;
for(ll j=i-1;j>=max(i-d2,0ll);j--)
{
cnt++;
for(ll a=0;a<=n1;a++)
{
for(ll b=0;b<=n2;b++)
{
ll c=n1+n2+n3-i-a-b;
if(c<=n3&&c>=0)
{
if(b+cnt>n2)continue;
if(j==0&&dp[i][a][b][c][1]==0)dp[i][a][b][c][1]+=1;
if(b+cnt<=n2)dp[i][a][b][c][1]=(dp[i][a][b][c][1]+dp[j][a][b+cnt][c][0])%MOD;
if(b+cnt<=n2)dp[i][a][b][c][1]=(dp[i][a][b][c][1]+dp[j][a][b+cnt][c][2])%MOD;
}
}
}
}
cnt=0;
for(ll j=i-1;j>=max(i-d3,0ll);j--)
{
cnt++;
for(ll a=0;a<=n1;a++)
{
for(ll b=0;b<=n2;b++)
{
ll c=n1+n2+n3-i-a-b;
if(c<=n3&&c>=0)
{
if(c+cnt>n3)continue;
if(j==0&&dp[i][a][b][c][2]==0)dp[i][a][b][c][2]+=1;
if(c+cnt<=n3)dp[i][a][b][c][2]=(dp[i][a][b][c][2]+dp[j][a][b][c+cnt][0])%MOD;
if(c+cnt<=n3)dp[i][a][b][c][2]=(dp[i][a][b][c][2]+dp[j][a][b][c+cnt][1])%MOD;
}
}
}
}
}
ll output=0;
output=(output+dp[n1+n2+n3][0][0][0][0])%MOD;
output=(output+dp[n1+n2+n3][0][0][0][1])%MOD;
output=(output+dp[n1+n2+n3][0][0][0][2])%MOD;
printf("%lld\n",output);
}
}
本文解析了一道经典的算法题——喂猴子。题目要求在给定各种水果数量的情况下,设计喂食方案,确保不会连续超过特定天数喂同一种水果。文章详细介绍了使用动态规划解决该问题的方法。
450

被折叠的 条评论
为什么被折叠?



