The recursive formula for the Catalan number is: f (n) = f (n-1) * (4*n-2)/(n + 1);
Since n is the maximum of 100, it is necessary to use an array to hold each of the Catalan numbers. You need to use a large number of operations.
The following is the code for the AC:
# include <stdio.h>struct Catalan{int length;int len[100];}; struct Catalan a[101];int main () {a[0].length = 1; a[0].len[0] = 1; First 4 Catalan number a[1].length = 1; A[1].len[0] = 1;a[2].length = 1; A[2].len[0] = 2;a[3].length = 1; A[3].len[0] = 5;int I, j, K, Num, N, Yu, div;for (i = 4; I <=; i++) {num = 4 * i-2; k = 0; yu = 0; div = 0;a[i].len Gth = 0;for (j = 0; J < A[i-1].length; J + +)//According to the formula, multiply by 4 * i-2, using a one-way method. {A[i].len[k] = (a[i-1].len[j] * num + yu)% 10;yu = (a[i-1].len[j] * num + yu)/10;k++;} while (Yu)//The remainder is not 0; continue to fill {a[i].len[k] = yu% 10;yu = yu/10;k++;} A[i].length = k;for (j = a[i].length-1; J >= 0; j--)//based on the formula, divided by i + 1, using a large number divided by a decimal. {div = div * + a[i].len[j];a[i].len[j] = div/(i + 1);d IV = div% (i + 1);} while (!a[i].len[a[i].length])//Remove the predecessor 0.a[i].length--;a[i].length++;} while (scanf ("%d", &n)! = EOF) {for (i = a[n].length-1; I >= 0; i--) printf ("%d", A[n].len[i]);p rintf ("\ n");}return 0;}
Hangzhou Electric Acm1130--how Many Tree? Number of ~~catalan