The two questions are surprisingly similar. Alas, I plan to summarize the ACM step questions one by one.
Come on! ~~~~~
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1133
This question is still the number of Catalan. A little deformation. The formula is derived from C [I] [J] = (I + J) * (I + 1-j)/J/(I + 2-j) * C [I] [J-1];
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1131
Or catalan number, formula: C [I] [J] = (4n-2)/(n + 1) * C [I] [J-1]
1131 conut the treeCode and explanation:
# Include <stdio. h> # include <string. h ># include <iostream> using namespace STD; int num [101] [400]; int Len [101]; void Catalan () {int I, J; num [0] [0] = 1; num [1] [0] = 1; Len [0] = 1; int flag; for (I = 2; I <101; I ++) {// By 4n-2 int K = 4 * I-2; flag = 0; For (j = 0; j <= Len [I-1]; j ++) {int z = Flag + num [I-1] [J] * k; num [I] [J] = z % 10; flag = z/10;} while (FLAG) {num [I] [J ++] = Flag % 10; flag/= 10;} Len [I] = J; flag = 0; // except n + 1 while (j> = 0) {int W = num [I] [J] + flag * 10; num [I] [J] = W/(I + 1); flag = w % (I + 1); j -- ;}} int fun () // calculate the base of the Catalan number. The factorial of M is {int I, K, M; For (M = 1; m <101; m ++) {for (I = 1; I <= m; I ++) {int flag = 0; For (k = 0; k <Len [m]; k ++) {int W = I * num [m] [k] + flag; num [m] [k] = w % 10; flag = W/10;} while (FLAG) {num [m] [k ++] = Flag % 10; flag/= 10;} Len [m] = K ;}} return 0 ;} int main () {Catalan (); fun (); int m; Len [1] = 1; int CNT = 1; while (CIN> M & M) {int I = Len [m]-1; while (I> = 0) cout <num [m] [I --]; cout <Endl;} return 0 ;}
1133buy the ticketCode and explanation:
# Include <stdio. h> # include <string. h> # include <iostream> using namespace STD; int C [101] [101] [400]; int Len [101] [101]; int Catalan () {memset (Len, 0, sizeof (LEN); memset (C, 0, sizeof (c); int I, j, k; for (I = 1; I <= 100; I ++) {C [I] [0] [0] = 1; Len [I] [0] = 1; // multiply (I + J) * (I + 1-j) for (j = 1; j <= I; j ++) {int flag = 0; for (k = 0; k <Len [I] [J-1]; k ++) {int W = (I + J) * (I + 1-j) * C [I] [J-1] [k] + flag; C [I] [J] [k] = w % 10; flag = W/10;} while (FLAG) {c [I] [J] [K ++] = Flag % 10; flag/= 10;} Len [I] [J] = K; // except J * (I + 2-j) int z = J * (I + 2-j); While (k> = 0) {int y = C [I] [J] [k] + flag * 10; c [I] [J] [k] = y/z; flag = Y % Z; k -- ;}} return 0 ;} int fun () // calculate the base of the Catalan number. The factorial of M is then multiplied by the factorial of N {int I, K, M, N; For (M = 1; m <101; M ++) {// when n = 0 for (I = 1; I <= m; I ++) {int flag = 0; For (k = 0; k <Len [m] [0]; k ++) {int W = I * C [m] [0] [k] + flag; C [m] [0] [k] = w % 10; flag = W/10;} while (FLAG) {C [m] [0] [k ++] = Flag % 10; flag/= 10 ;} len [m] [0] = K;} For (n = 1; n <= m; n ++) {for (I = 1; I <= m; I ++) {int flag = 0; For (k = 0; k <Len [m] [N]; k ++) {int W = I * C [m] [N] [k] + flag; C [m] [N] [k] = w % 10; flag = W/10;} while (FLAG) {C [m] [N] [k ++] = Flag % 10; flag/= 10 ;} len [m] [N] = K;} for (I = 1; I <= N; I ++) {int flag = 0; For (k = 0; k <Len [m] [N]; k ++) {int W = I * C [m] [N] [k] + flag; c [m] [N] [k] = w % 10; flag = W/10;} while (FLAG) {c [m] [N] [k ++] = Flag % 10; flag/= 10;} Len [m] [N] = K ;}}} return 0;} int main () {Catalan (); fun (); int M, N; int CNT = 1; while (CIN> m> N & (m + n )! = 0) {printf ("test # % d: \ n", CNT ++); If (M <n) cout <"0" <Endl; else {int I = Len [m] [N]-1; while (I> = 0) cout <C [m] [N] [I --]; cout <Endl ;}} return 0 ;}