卡特兰数应用

catalan数

其前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845。。。。。

令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)=((4*n-2)/(n+1))*h(n-1);   

该递推关系的解为:   h(n)=C(2n,n)/(n+1) (n=1,2,3,...)

它的适用情况有:

1、取物2n个,物品分a,b两种,任意时刻手中的a物品数<b物品数,的方法数为h(n)。

2、把(n+2)边形分割成若干个三角形组面积组合的方法数为h(n)。

3、一圆环上有2n个点两两连线不交叉的方法数为h(n)。

poj2084:应用场景3

代码如下:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static BigInteger catalan(int n){
		if(n==0||n==1)
			return BigInteger.ONE;
		return (catalan(n-1).multiply(BigInteger.valueOf((4*n-2)))).divide(BigInteger.valueOf(n+1));
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(true){
			int n = in.nextInt();
			if(n==-1)
				break;
			System.out.println(catalan(n));
		}
	}

}
原文地址:https://www.cnblogs.com/wt20/p/5783601.html