您的位置:首页 > Web前端

sdut3565——Feed the monkey(记忆化DP)

2017-04-29 20:20 363 查看


Feed the monkey

Time Limit: 2000MS Memory Limit: 131072KB

Submit Statistic Discuss


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

 “浪潮杯”山东省第七届ACM大学生程序设计竞赛

题目大意:

一共有三种食物,每一种食物的数量已知,每天吃任意一个食物,每一种的食物不能连续吃D1.D2.D3天,问可行的方案数有多少。

思路:

思路:记忆化搜索,dp[num1][num2][num3][pre][sum],num1 num2 num3表示三种水果剩余数,pre表示上一个放的是哪个水果,sum表示上一个水果已经连续放了多少天。

#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>

#include <cmath>

#include <algorithm>

#include <vector>

#include <map>

#include <string>

#include <stack>

using namespace std;

typedef long long ll;

#define PI 3.1415926535897932

#define E 2.718281828459045

#define INF 0xffffffff//0x3f3f3f3f

#define mod 1000000007

const int M=1005;

int n,m;

int cnt;

int sx,sy,sz;

int mp[1000][1000];

int pa[M*10],rankk[M];

int head[M*6],vis[M*100];

int dis[M*100];

ll prime[M*1000];

bool isprime[M*1000];

int lowcost[M],closet[M];

char st1[5050],st2[5050];

int len[M*6];

typedef pair<int ,int> ac;

//vector<int> g[M*10];

int dp[55][55][55][5][55];

int has[10500];

int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};

int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

void getpri()

{

    ll i;

    int j;

    cnt=0;

    memset(isprime,false,sizeof(isprime));

    for(i=2; i<1000000LL; i++)

    {

        if(!isprime[i])prime[cnt++]=i;

        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)

        {

            isprime[i*prime[j]]=1;

            if(i%prime[j]==0)break;

        }

    }

}

struct node

{

    int v,w;

    node(int vv,int ww)

    {

        v=vv;

        w=ww;

    }

};

vector<int> g[M*100];

char str[100005];

int ind[2550];

int bit[50];

int K;

int D1,D2,D3;

int dfs(int n1,int n2,int n3,int pre,int sum)

{

    int ans=0;

    if(n1<0||n2<0||n3<0)return 0;

    if((pre==0&&sum>D1)||(pre==1&&sum>D2)||(pre==2&&sum>D3))return 0;

    if(n1==0&&n2==0&&n3==0)return 1;

    if(dp[n1][n2][n3][pre][sum]!=-1)return dp[n1][n2][n3][pre][sum];//坑啊,这句放错位置RE到毁灭。。。,以后记住了

    if(pre==0)

    {

        ans=(ans+dfs(n1-1,n2,n3,0,sum+1))%mod;

        ans=(ans+dfs(n1,n2-1,n3,1,1))%mod;

        ans=(ans+dfs(n1,n2,n3-1,2,1))%mod;

    }

    else if(pre==1)

    {

        ans=(ans+dfs(n1-1,n2,n3,0,1))%mod;

        ans=(ans+dfs(n1,n2-1,n3,1,sum+1))%mod;

        ans=(ans+dfs(n1,n2,n3-1,2,1))%mod;

    }

    else if(pre==2)

    {

        ans=(ans+dfs(n1-1,n2,n3,0,1))%mod;

        ans=(ans+dfs(n1,n2-1,n3,1,1))%mod;

        ans=(ans+dfs(n1,n2,n3-1,2,sum+1))%mod;

    }

    return dp[n1][n2][n3][pre][sum]=ans;

}

int main()

{

    int i,j,k,t;

    ll l,r;

    int cas=0;

    int N1,N2,N3;

    //memset(dp,-1,sizeof(dp));

    scanf("%d",&t);

    while(t--)

    {

        memset(dp,-1,sizeof(dp));

        scanf("%d%d%d%d%d%d",&N1,&N2,&N3,&D1,&D2,&D3);

        int ans=0;

        for(i=0; i<3; i++)

        {

            ans=(ans+dfs(i==0?N1-1:N1,i==1?N2-1:N2,i==2?N3-1:N3,i,1))%mod;

        }

        printf("%d\n",ans%mod);

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: