标签:
卡特兰公式为:
(n+2)record[n+1]=(4n+2)record[n]
#include<stdio.h> #include<string.h> int record[110][110]; int a1[110],weishu[110]; void cheng(int a){ int i,m,c,chushu,temp; m=weishu[a-1]; c=0; for(i=0;i<=m;i++){ a1[i]=a1[i]*(4*a-2)+c; c=a1[i]/10000; a1[i]%=10000; } if(c>0){ ++m; a1[m]=c; } chushu=a+1,temp=a1[m]; if(m==0) a1[0]/=chushu; else{ for(i=m;i>=0;--i){ if(temp<chushu){ if(i==m) --m; else a1[i]=0; } else { a1[i]=temp/chushu; temp%=chushu; } temp=temp*10000+a1[i-1]; } } weishu[a]=m; for(i=0;i<=weishu[a];++i) record[a][i]=a1[i]; } int main() { int a,i,n; record[1][0]=1; weishu[1]=0; a1[0]=1; for(i=2;i<=100;i++) cheng(i); while(scanf("%d",&n)&&n!=-1){ printf("%d",record[n][weishu[n]]); for(i=weishu[n]-1;i>=0;--i) { printf("%.4d",record[n][i]); } printf("\n"); } return 0; }
java大数:
import java.util.*; import java.math.*; public class Main{ public static void main(String[] args){ int n; Scanner in=new Scanner(System.in); BigInteger one=BigInteger.ONE; BigInteger four=new BigInteger("4"); BigInteger two=new BigInteger("2"); BigInteger st=null; BigInteger t=null; while(in.hasNextInt()){ n=in.nextInt(); if(n==-1) break; t=BigInteger.ONE; for(int i=2;i<=n;i++) { st=new BigInteger(""+i); t=t.multiply(st.multiply(four).subtract(two)).divide(st.add(one)); } System.out.println(t); } } }
hdu1134 Game of Connections(卡特兰数)
标签:
原文地址:http://blog.csdn.net/a197p/article/details/46502829