import java.math.BigInteger; 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(); if(n==-1) break; System.out.println(h[n]); } } }
解题思路: 看着数据,有点儿像卡特兰数,直接写了看,就过了。。。 前几项为 (OEIS中的数列A000108): 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452, ... 总结下卡特兰数,不然还晕乎乎的,做了很多卡特兰数的题,公式倒是记住了,但是对这个卡特兰数的意义理解并不是很多,现在,总结下吧: 卡特兰数实质:h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2) 经常用的公式是: 令h(1)=1,h(0)=1,catalan数满足递归式: h(n)=h(n-1)*(4*n-2)/(n+1); 卡特兰数模型: 1.出栈次序问题 一个栈(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列? 2.凸多边形的三角剖分问题 求将一个凸多边形区域分成三角形区域的方法数。 类似:一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路? 类似:在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数? 3.用给定节点组成二叉树的问题 (能构成h(N)个)
转载于:https://www.cnblogs.com/cchun/archive/2011/11/02/2520156.html