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?
There are several test cases. Each case contains a number N (1 <= N <= 1000) indicating the pairs of brackets.
For each case, please output the answer mod 1,000,000,007.
#include <iostream> #include <stdio.h> #define NUM 1000000007 using namespace std; long long int a[1010]; int main() { int n; a[0]=1; for(int i = 1; i <= 1000; i++) { for(int j = 0; j < i; j++) { a[i]=a[i]+a[j]*a[i-j-1]%NUM; a[i]=a[i]%NUM; } } //cout<<a[1]<<endl<<a[2]<<endl<<a[3]<<endl<<a[4]<<endl; while(cin>>n){ cout<<a[n]<<endl; } return 0; }
說好的卡特兰数
just 坑爹 比賽的時候用的h(n)=h(n-1)*(4*n-2)/(n+1);這個遞推公式,這個是不適合大數取模運算的