题面
Bobo has a string of length 2(n + m) which consists of characters `A` and `B`. The string also has a fascinating property: it can be decomposed into (n + m) subsequences of length 2, and among the (n + m) subsequences n of them are `AB` while other m of them are `BA`. Given n and m, find the number of possible strings modulo (109+7)(109+7).
输入描述:
The input consists of several test cases and is terminated by end-of-file.Each test case contains two integers n and m.* 0≤n,m≤1030≤n,m≤103* There are at most 2019 test cases, and at most 20 of them has max{n,m}>50max{n,m}>50.
输出描述:
For each test case, print an integer which denotes the result.
示例1
输入
1 2
1000 1000
0 0
输出
13
436240410
1
题目链接
参考链接
题意
问:有多少种由n个‘AB’序列和m个’BA’序列构成的长度为2(n+m)的字符串?
分析
任何情况下,将前n个A用于‘AB’中的A,前m个B用于‘BA’中的B。
Dp[i][j]表示当前字符串有i个A,j个B暂时合法的长度为(i+j)的字符串的种数。
DP[i][j]由X和Y两部分组成。DP[i][j]=X+Y;
X表示,第i+j个字符为A且含有i个A,j个B的合法字符串种数,即合法情况下含有i-1个A,j个B的字符串种数。合法条件(即A的个数合乎条件):①A的个数小于等于n,②去掉n个A后,A的个数小于字符串中用于BA的B的个数(min(j,m))
Y表示,第i+j个字符为B且含有i个A,j个B的合法字符串种数,即合法情况下含有i个A,j-1个B的字符串种数。合法条件(即B的个数合乎条件):①B的个数小于等于m,②去掉m个B后,B的个数小于字符串中用于AB的A的个数(min(i,n))
程序
#include<stdio.h>
#include<iostream>
using namespace std;
#define mod 1000000007
#define maxn 2005
int n,m,length;
int dp[maxn][maxn];
void init()
{
length=n+m;
for(int i=0;i<=length;i++)
for(int j=0;j<=length;j++)
dp[i][j]=0;
dp[0][0]=1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=0;i<=length;i++)
{
for(int j=0;j<=length;j++)
{
if(i<n||i-n<min(j,m))
{
dp[i+1][j]+=dp[i][j];
dp[i+1][j]%=mod;
}
if(j<m||j-m<min(i,n))
{
dp[i][j+1]+=dp[i][j];
dp[i][j+1]%=mod;
}
}
}
printf("%d\n",dp[length][length]);
}
return 0;
}
2019年07月23日 09:42:27