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

本文介绍了一道经典的动态规划问题——猴子喂食问题。题目要求计算在给定每种水果数量及连续喂食限制的情况下,所有可能的喂食方案总数。通过使用四维动态规划数组dp[a][b][c][k]来存储状态,最终输出模1e9+7后的结果。

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


题意

给出三种水果以及每种水果不能连续的数量,求总共有多少种组合的方式。


思路

dp[a][b][c][k] 代表当前剩余第一种水果 a 个,剩余第二种水果 b 个,剩余第三种水果 c 个,并且以 k 结尾所有的组合数。

枚举所有的 a,b,cdp[s][b][c][0] 可以由 dp[a][b][c][1]dp[a][b][c][2] 转移而来,其中 s 为保证第一种水果最多连续不超过 d[0] 的水果数目。

因此 s 取值范围为: [max(0,a-d[0]),a)

第二种水果和第三种水果的转移也类似,输出 mod 1e9+7 之后的结果。


AC 代码

#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
typedef long long LL;
LL dp[51][51][51][3];
int n[3],d[3];

int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        for(int i=0; i<3; i++)
            cin>>n[i];
        for(int i=0; i<3; i++)
            cin>>d[i];
        for(int a=n[0]; a>=0; a--)
        {
            for(int b=n[1]; b>=0; b--)
            {
                for(int c=n[2]; c>=0; c--)
                {
                    for(int s=max(0,a-d[0]); s<a; s++)
                    {
                        if(a==n[0]&&b==n[1]&&c==n[2])
                            dp[s][b][c][0]=(dp[s][b][c][0]+1)%mod;
                        else
                            dp[s][b][c][0]=(dp[s][b][c][0]+dp[a][b][c][1]+dp[a][b][c][2])%mod;
                    }
                    for(int s=max(0,b-d[1]); s<b; s++)
                    {
                        if(a==n[0]&&b==n[1]&&c==n[2])
                            dp[a][s][c][1]=(dp[a][s][c][1]+1)%mod;
                        else
                            dp[a][s][c][1]=(dp[a][s][c][1]+dp[a][b][c][0]+dp[a][b][c][2])%mod;
                    }
                    for(int s=max(0,c-d[2]); s<c; s++)
                    {
                        if(a==n[0]&&b==n[1]&&c==n[2])
                            dp[a][b][s][2]=(dp[a][b][s][2]+1)%mod;
                        else
                            dp[a][b][s][2]=(dp[a][b][s][2]+dp[a][b][c][0]+dp[a][b][c][1])%mod;
                    }
                }
            }
        }
        cout<<((dp[0][0][0][0]+dp[0][0][0][1])%mod+dp[0][0][0][2])%mod<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值