Shandong province Seventh annual ACM provincial game Feed the Monkey (DP) __ Dynamic Planning

Source: Internet
Author: User
Problem Description

Alice has a monkey, she must feed fruit to the monkey 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 days, consecutive for more peaches than D2 D Ays, or apples for the more than D3 consecutive days. Now Alice has N1 bananas, N2 peaches and N3 apples, please help her the number of calculate to feed the schemes.


Input

Multiple test cases. The ' The ' 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 each case. The number of schemes to feeds the monkey during (n1+n2+n3) days.

The answer is too large. Should mod 1000000007.


Example Input

1
2 1 1 1 1 1


Example Output

6


the

Give three kinds of fruit and each fruit can not be continuous quantity, ask the total number of combinations of the way.


train of Thought

Dp[a][b][c][k] represents the current remaining first fruit A, the remaining second fruit B, the remaining third fruit C, and at the end of all combinations of K.

Enumeration of all A,b,c, dp[s][b][c][0] can be transferred from dp[a][b][c][1] and dp[a][b][c][2, where S is the number of fruits to ensure that the first fruit is no more than d[0 in succession.

So the S value range is: [Max (0,a-d[0]), a).

The transfer of the second fruit and the third fruit is similar, output after the MoD 1e9+7 results.


AC Code

#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]&&
                        AMP;B==N[1]&AMP;&AMP;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&L
    t;<endl;
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.