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
****************************************************************/