题目链接:https://ac.nowcoder.com/acm/contest/881/E
链接:https://ac.nowcoder.com/acm/contest/881/E
来源:牛客网
ABBA
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
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).
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
输出
复制13 436240410 1
题目大意: 有多少个长度为 2(n+m)2(n+m) 的AB序列,可以划分成 nn 个 ABAB 子序列,mm 个 BABA 子序列。
思路:全在代码里了
看代码:
#include<iostream> using namespace std; typedef long long LL; const int mod=1e9+7; const int maxn=1e3+5; LL dp[maxn<<1][maxn<<1]; int main() { LL N,M; while(cin>>N>>M) { for(int i=0;i<=N+M;i++) for(int j=0;j<=N+M;j++) dp[i][j]=0; dp[0][0]=1;//初始化 for(int i=0;i<=N+M;i++) { for(int j=0;j<=N+M;j++) { /** 此时有i个A 考虑能不能再放A 如果i+1-N<=0代表此时的A还不够N个 所以一定可以放 因为最开始N个A 是构成AB的 当i+1-N>0时 要判断是否j>=i+1-N 因为此时放的A 前面一定要有B与 自己构成BA */ if(j>=i+1-N) dp[i+1][j]=(dp[i+1][j]+dp[i][j])%mod; /** 其实这个也是同理 但是还是说一下 此时有j个B 考虑能不能再放B 如果j+1-M<=0 代表此时j还不足M个 所以一定能够放 因为最开始M个B 是构成BA的 当j+1-M>=0时 要判断是否i>=j+1-M 因为此时放的B 前面一定要有A与 自己构成AB */ if(i>=j+1-M) dp[i][j+1]=(dp[i][j+1]+dp[i][j])%mod; // cout<<dp[i+1][j]<<endl; // cout<<dp[i][j+1]<<endl; } } cout<<dp[N+M][N+M]<<endl; } return 0; }