链接:https://ac.nowcoder.com/acm/contest/881/E
来源:牛客网
题目描述
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)
输入描述:
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≤103
输出描述:
For each test case, print an integer which denotes the result.
输入
1 2 1000 1000 0 0
输出
13 436240410 1
给出n,m,构造字符串可以分成n个AB,m个BA,求方案数
每个A一定是先构成AB,后构成BA
从前到后判断每个位置放A或放B
放A,如果有AB还没A,就把这个A分给AB
接下来看BA是否有 分到了B但还没有分到A的,如果有,就把这个A分给他
dp[i][j]表示放了i个A,j个B满足条件的方案数
A:不能直接放的是 i-n>=j 即现在A用于组成BA的数量大于B的数量,无法构成m个BA
所以,条件i-n<j,dp[i+1][j]+=dp[i][j]
B同理
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
ll num[2005][2005];
int main()
{
ll n,m;
while(scanf("%lld%lld",&n,&m)!=EOF){
for(ll i=0; i<=n+m; i++){
for(ll j=0; j<=n+m; j++){
num[i][j]=0;
}
}
num[0][0]=1;
for(ll i=0; i<=n+m; i++){
for(ll j=0; j<=n+m; j++){
if(i-n<j)
num[i+1][j]=(num[i][j]+num[i+1][j])%mod;
if(j-m<i)
num[i][j+1]=(num[i][j]+num[i][j+1])%mod;
}
}
printf("%lld\n",num[n+m][n+m]);
}
return 0;
}