题目大意:
就是给你1到N个数,让你求他能构成多少种二叉树;
题目分析:
这里又是一种组合数学里的重要知识点!Catalan数的应用。
Catelan有直接的公式,用java的BigInteger类就可以迅速搞掉了,用c/c++要慢慢模拟,不过速度会更快~各有特色吧。Java用了140MS
//令h(1)=1,h(0)=1,catalan数满足递归式:
//h(n)=(4*n-2)/(n+1)*h(n-1);
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String []args)
{
Scanner cin=new Scanner(System.in);
BigInteger h[]=new BigInteger[1005];
h[0]=new BigInteger("1");
h[1]=new BigInteger("1");
BigInteger one=new BigInteger("1");
BigInteger four=new BigInteger("4");
BigInteger two=new BigInteger("2");
for(int i=2;i<1005;i++)
{
String str=String.valueOf(i);
BigInteger n=new BigInteger(str);
BigInteger temp=new BigInteger("0");
h[i]=(n.multiply(four).subtract(two)).multiply(h[i-1]).divide(n.add(one));
//h[i]=temp.divide(n.add(one));
//h[i]=h[i-1].multiply(4*n-1).divide(n.add());
}
while(cin.hasNext())
{
//令h(1)=1,h(0)=1,catalan数满足递归式:
//h(n)=(4*n-2)/(n+1)*h(n-1);
int n=cin.nextInt();
System.out.println(h[n]);
}
}
}