Brackets
This year MK is 5 years old. So he decides to learnsome arithmetic. But he was confused by how towrite the brackets. He has already known that the brackets should match whenwriting them correctly. Such as “()(())” is correct but “())(” is not.
Theproblem is that, if there are N pairs of brackets, how many ways that MK can writethem correctly?
Input
There areseveral 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
大神说这个是裸的卡特兰数的题,经过大神提醒知道卡特兰数,自己查了下。
分享个有关卡特兰数的连接 http://buptdtt.blog.51cto.com/2369962/832586
知道卡特兰数这题就好做了,直接公式计算就出来了。
还有个麻烦点的地方就是mod 1,000,000,007。需要边计算边mod。
#include<iostream>
using namespace std;
long long f[1003];
int main()
{
int n;
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] % 1000000007;
f[i] %= 1000000007;
}
}
while(cin>>n) cout<<f[n]<<endl;
return 0;
}