catalan数 /* Author: ACb0y Date: 2010年11月13日17:16:51 Type: catalan数的应用 递推公式为:C( n ) = n * (4*n - 2)* C(n - 1)/(n + 1); ProblemId: hdu 1131 Count the Trees Result: 3190360 2010-11-13 17:13:59 Accepted 1131 0MS 588K 1123 B G++ ACb0y */ #include <iostream> using namespace std; struct node { int digit[800]; int len; }; node ans[101]; void print_ans(int pos) { node & cur = ans[pos]; for (int i = cur.len - 1; i >= 0; --i) { printf("%d", cur.digit[i]); } printf("/n"); } void get_ans() { int i, j; int up; ans[1].len = 1; ans[1].digit[0] = 1; for (i = 2; i <= 100; ++i) { node & pre = ans[i - 1]; node & cur = ans[i]; for (j = 0; j < pre.len; ++j) { cur.digit[j] = pre.digit[j] * (4 * i - 2) * i; } cur.len = pre.len; for (j = up = 0; j < cur.len; ++j) { up += cur.digit[j]; cur.digit[j] = up % 10; up /= 10; } while (up) { cur.digit[cur.len] = up % 10; up /= 10; cur.len++; } up = 0; for (j = cur.len - 1; j >= 0; --j) { up *= 10; up += cur.digit[j]; cur.digit[j] = up / (i + 1); up %= i + 1; } while (cur.digit[cur.len - 1] == 0) { cur.len--; } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int n; get_ans(); while (scanf("%d", &n) != EOF) { if (n == 0) { break; } else { print_ans(n); } } return 0; }