题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1023
catalan数,公式:h[n] = h[n-1]*(4*n-2)/(n+1);
需要用到高精度,对运算符重载一下。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 110; struct BigInt { short s[MAXN*2]; int l; }c[MAXN]; BigInt operator *(BigInt a, int b) { int i; for(i = 0; i < a.l; ++i) a.s[i] *= b; for(i = 0; i < a.l; ++i) { a.s[i+1] += a.s[i]/10; a.s[i] %= 10; } while(a.s[a.l] != 0) { a.s[a.l+1] = a.s[a.l]/10; a.s[a.l] %= 10; a.l++; } return a; } BigInt operator /(BigInt a, int b) { int i; for(i = a.l-1; i > 0; --i) { a.s[i-1] += a.s[i]%b * 10; a.s[i] /= b; } a.s[0] /= b; while(a.s[a.l-1] == 0) a.l--; return a; } void Print(BigInt a) { int i; for(i = a.l-1; i >= 0; --i) printf("%d", a.s[i]); printf("\n"); } int main() { int i, n; c[0].l = 1, c[0].s[0] = 1; for(i = 1; i < MAXN; ++i) c[i] = c[i-1] * (4*i - 2)/(i+1); while(~scanf("%d", &n)) { Print(c[n]); } return 0; }