http://acm.hdu.edu.cn/showproblem.php?pid=1023
卡塔兰(Catalan)数
我的代码
1 #include <stdio.h>
2 #include <string.h>
3 const int L=100,base=100000;
4 void print(int *a)
5 {
6 printf("%d",a[a[0]]);
7 for (int i=a[0]-1;i>0;i--) printf("%05d",a[i]);
8 printf("\n");
9 }
10 void mult(int *a,int b)
11 {
12 a[1]*=b;
13 for (int i=2;i<=a[0];i++)
14 {
15 a[i]=a[i]*b+a[i-1]/base;
16 a[i-1]%=base;
17 }
18 while (a[a[0]]>=base)
19 {
20 a[0]++;
21 a[a[0]]=a[a[0]-1]/base;
22 a[a[0]-1]%=base;
23 }
24 }
25 void divi(int *a,int b)
26 {
27 int t=0,i;
28 for (i=a[0];i>0;i--)
29 {
30 t=t*base+a[i];
31 a[i]=t/b;
32 t%=b;
33 }
34 while (a[a[0]]==0) a[0]--;
35 }
36 int main()
37 {
38 int n,a[L];
39 while (scanf("%d",&n)!=EOF)
40 {
41 n+=2;
42 memset(a,0,sizeof(a));
43 a[a[0]=1]=1;
44 int i;
45 for (i=n;i<=2*n-4;i++) mult(a,i);
46 for (i=2;i<=n-2;i++) divi(a,i);
47 print(a);
48 }
49 return 0;
50 }