Problem A : King Is a Dull Boy
Description
King is a dull boy and not good at math. One day he met a simple problem but couldn’t find the answer, so he asked you for help. Here is the question.
Given n pairs of parentheses (‘(’ and ‘)’) and m pairs of square brackets (‘[’ and ‘]’), can you find the number of all well-formed expressions of brackets?
For example, if n = 1 and m = 1, there are totally 4 different well-formed expressions of brackets,
Note that expressions like “([]”, "[)", "([)]" or “[(])” are not well-formed.
Input
There are several test cases, each of which consists of two non-negative integers n and m, where 0 ≤ n + m ≤ 8.
Output
For each test case, output the number of all well-formed expressions of brackets.
Sample Input
1 1 2 0
Sample Output
4 2
题意:有两种括号,求正确的括号匹配数。
解题思路:卡特兰数*排列组合。
#include<iostream>
using namespace std;
typedef long long int ll;
long long int C(ll a,ll b){
ll sum=1;
for(ll i=a;i>=1;i--){
sum*=i;
}
ll c=1;
for(ll i=b;i>=1;i--){
c*=i;
}
for(ll i=a-b;i>=1;i--){
c*=i;
}
return sum/c;
}
int main(){
int N,M;
while(~scanf("%d%d",&N,&M)){
ll tot=0;
int sum=N+M;
tot=C(2*sum,sum)-C(2*sum,sum-1);
tot*=C(sum,N);
cout<<tot<<endl;
}
return 0;
}