做法:先固定一个点例如1,然后分别连1-2,1-3,1-4.......,这样线段两边各有f1和f2种情况,因此有公式f(n)=f(0)*f(n-2) + f(2)*f(n-4) + f(4)*f(n-8) .....+ f(n-2)*f(0),一看,这就是大名鼎鼎的Catalan数!
令h(1)=1,h(0)=1,Catalan数满足递归式:
h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2) 另类递归式: h(n)=h(n-1)*(4*n-2)/(n+1); //此题本人采用此公式! 该递推关系的解为:h(n)=C(2n,n)/(n+1) (n=1,2,3,...)
import java.io.*;
import java.math.*;
import java.util.*;
public class Main{
public static void main(String[] args){
int n;
Scanner cin = new Scanner(new BufferedInputStream(System.in));
while(cin.hasNext()){
n = cin.nextInt();
if(n==-1)break;
BigInteger a[] = new BigInteger[110];
int i;
a[0] = a[1] = new BigInteger("1") ;
for(i=2;i<=n;i++){
a[i] = a[i-1].multiply( BigInteger.valueOf( (4*i-2)) );
a[i] = a[i].divide(BigInteger.valueOf(i+1));
}
System.out.println(a[n]);
}
}
}