HDU 1130 How Many Trees?

裸的卡特兰数

C++
#include<iostream> #include<cstdio> using namespace std; #define base 10000 #define len 100 void multiply(int a[],int max,int b) { int i,array=0; for(i=max-1;i>=0;i--) { array+=b*a[i]; a[i]=array%base; array/=base; } } void divide(int a[],int max,int b) { int i,div=0; for(i=0;i<max;i++) { div=div*base+a[i]; a[i]=div/b; div%=b; } } int main() { int n,i; int a[101][len]; memset(a[1],0,len*sizeof(int)); for(i=2,a[1][len-1]=1;i<101;i++) { memcpy(a[i],a[i-1],len*sizeof(int)); multiply(a[i],len,4*i-2); divide(a[i],len,i+1); } while(scanf("%d",&n)!=EOF) { for(i=0;i<len&&a[n][i]==0;i++); printf("%d",a[n][i++]); for(;i<len;i++) { printf("%04d",a[n][i]); } printf("\n"); } return 0; }
pascal
const modnum=10000; type arraytype=array[0..100000] of longint; var s:string; i,j,l,m,n,le:longint; h:arraytype; procedure mul(var h:arraytype; k:longint); var j:longint; begin for j:=0 to le do h[j]:=h[j]*k; for j:=0 to le-1 do begin h[j+1]:=h[j+1]+h[j] div modnum; h[j]:=h[j] mod modnum; end; while h[le]>modnum do begin inc(le); h[le]:=h[le-1] div modnum; h[le-1]:=h[le-1] mod modnum; end; end; procedure devide(var h:arraytype; k:longint); var d,o,r,i:longint; begin r:=0; for i:=le downto 0 do begin d:=modnum*r+h[i]; h[i]:=d div k; r:=d mod k; end; end; begin readln(n); h[0]:=1; for i:=1 to n do begin mul(h,i+n); devide(h,i); end; devide(h,n+1); while h[le]=0 do dec(le); write(h[le]); for i:=le-1 downto 0 do begin str(h[i],s); while length(s)<4 do s:='0'+s; write(s); end; end.

 

posted @ 2014-01-18 17:45  forever97  阅读(269)  评论(0编辑  收藏  举报