[第一次训练]Brackets

Brackets 

This year MK is 5 years old. So he decides to learn some arithmetic. But he was confused by how to write the brackets. He has already known that the brackets should match when writing them correctly. Such as “()(())” is correct but “())(” is not.

The problem is that, if there are N pairs of brackets, how many ways that MK can write them correctly?           

Input 

   There are several test cases. Each case contains a number N (1 <= N <= 1000) indicating the pairs of brackets.

Output 

For each case, please output the answer mod 1,000,000,007.

Sample Input 

5

7

Sample Output 

42

429



解题报告

此题为 Catalan number(卡特兰数)的题目,此题套用通用公式令h(0)=1,h(1)=1,catalan数满足递推式 h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0)(n>=2),,然后在边乘边模即可。  代码如下:
#include<stdio.h>
const int maxn = 1000 + 5;
const int MOD = 1000000007;
/*Catalan number*/

int n;
long long f[maxn];

int main() {
    f[0] = 1;
    for(int i = 1; i <= 1000; i++) {
        for(int j = 0; j < i; j++) {
            f[i] += f[j] * f[i - j - 1] % MOD;
            f[i] %= MOD;
        }
    }
    while(scanf("%d", &n) == 1) {
        printf("%I64d\n", f[n]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值