山东省第七届ACM省赛 Feed the monkey(dp)

13 篇文章 0 订阅
13 篇文章 0 订阅

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值