2018年上海金马五校程序设计竞赛 Problem A : King Is a Dull Boy

Problem A : King Is a Dull Boy


Submit (Out of Contest)

Time Limit: 3 s

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,

“([])”, “()[]”, “[()]” and “[]()”.


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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值