Brackets解题报告

来源:互联网 发布:淘宝公益宝贝可以买吗 编辑:程序博客网 时间:2024/05/10 08:13

题目摘要:This year MK is 5 years old. So hedecides to learn somearithmetic But he wasconfused by how to write the brackets. He has already known that the bracketsshould match when writing them correctly. Such as “()(())” is correct but “())(”is not.

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

题目大意:给出N对括号,输出正确的组合方式数

输入输出要求

Input

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

 

Output

For each case, please output the answer mod1,000,000,007.

 

输入输出样例

Sample Input :

5

7

Sample Output :

42

429

 

解题思路:结果就是一组卡特兰数,用公式输出结果就行,由于测试的数据很大,所以边乘边模。

代码

#include<iostream>

using namespace std;

const int maxn=1000+5;

const int MOD=1000000007;

 

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(cin>>n)

{

cout<<f[n]<<endl;

}

return 0;

}

解题感想:开始用递归的公式做的,数据一大就递不出结果了,感觉不会再爱递归了……

原创粉丝点击