17.4.8.Problem C: 六队-某愚蠢的字符串_数位dp

Time Limit: 1 Sec Memory Limit: 128 MB
Description
有字母a、b、c、d分别na、nb、nc、nd个,求能组成多少个满足下列条件的字符串。

条件:对于任意该字符串的前缀,记a、b、c、d的个数为ma、mb、mc、md,则ma>=mb>=mc>=md;

Input
T(表示数据组数)

每组一行na nb nc nd (输入数据都小于等于40)

Output
每组一行,每行一个答案 结果对1e9+7取模

Sample Input
1
2 2 2 0
Sample Output
5

分析:Sample 中 5 种包括 aabbcc aabcbc ababcc abacbc abcabc
用DP过

#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
#define mod 1000000007
#define LL long long
LL dp[50][50][50][50];
LL solve(int a,int b,int c,int d)
{
    if(a<b||b<c||c<d)return 0;
    int n=a+b+c+d;
    memset(dp,0,sizeof(dp));
    dp[0][0][0][0]=1;
    for(int i=1;i<=n;i++)
    {
        for(int x1=i/4;x1<=i&&x1<=a;x1++)//从 i/4  开始 
        {
            for(int x2=0;x2<=x1&&x2<=b;x2++)
            {
                for(int x3=0;x3<=x2&&x3<=c;x3++)
                {
                    int x4=i-x1-x2-x3;
                    if(x4>x3)continue;
                    if(x1>x2)dp[x1][x2][x3][x4]+=dp[x1-1][x2][x3][x4];
                    if(x2>x3)dp[x1][x2][x3][x4]+=dp[x1][x2-1][x3][x4];
                    if(x3>x4)dp[x1][x2][x3][x4]+=dp[x1][x2][x3-1][x4];
                    if(x1>0)dp[x1][x2][x3][x4]+=dp[x1][x2][x3][x4-1];
                    dp[x1][x2][x3][x4]%=mod;
                }
            }
        }
    }
    return dp[a][b][c][d]%mod;
}
int main()
{
    int n;
    int a,b,c,d;
    cin>>n;
    while(n--)
    {
        cin>>a>>b>>c>>d;
        cout<<solve(a,b,c,d)<<endl;
    }
    return 0;
}
/**************************************************************
    Problem: 1730
    User: zjut_22
    Language: C++
    Result: Accepted
    Time:220 ms
    Memory:50504 kb
****************************************************************/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值