难度:4
算法:卡特林数
ans = C(2n , n) / (n+1)
用大数与整数的乘法和除法过的
更多有关卡特林数的资料:http://baike.baidu.com/view/1154333.htm?fr=ala0_1_1
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10001;
int n , s[maxn];
void init() {
memset(s , 0 ,sizeof(s));
s[0] = 1;
}
void mul(int a) {
int c = 0;
for(int i=0;i<maxn;i++) {
int tmp = s[i] * a + c;
s[i] = tmp % 10;
c = tmp / 10;
}
}
void sub(int a) {
int c = 0;
for(int i=maxn-1;i>=0;i--) {
int tmp = c * 10 + s[i];
s[i] = tmp / a;
c = tmp % a;
}
}
void output() {
int i = maxn - 1;
while(!s[i]) i --;
for(;i>=0;i--) printf("%d" , s[i]);
puts("");
}
int main() {
while(~scanf("%d" , &n)) {
init();
for(int i=n+2;i<=2*n;i++) mul(i);
for(int i=2;i<=n;i++) sub(i);
output();
}
return 0;
}