卡特兰数

递推式:

h(n) = h(0) * h(n-1) + h(1) * h(n-2) + …… + h(n-1)h(0);

 

递推关系的解:

h(n) = C(2n, n)/(n +1)

 

递推关系的另类解1:

h(n) = C(2n, n) – C(2n, n-1)

折线法证明法:

http://blog.sina.com.cn/s/blog_6917f47301010cno.html

 

另类递推式:

h(n) = h(n-1) * ((4*n-2)/(n+1))

 

卡特兰数的实现

package acm;

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

public class P1122 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        
        // 递归实现
        int result = Catalan(n);

        System.out.println(result);
        
        // 非递归实现
        BigInteger catalan[] = new BigInteger[n + 1];
        
        catalan[0] = new BigInteger("1");
        catalan[1] = new BigInteger("1");
        
        for(int i = 2; i< n +1; i++) {
            
            int x = 4 * i - 2 ;
            int y = i + 1;
            catalan[i] = catalan[i-1].multiply(new BigInteger("" + x)).divide(new BigInteger(""+ y));
        }

        System.out.println(catalan[n]);
    }

    // 递归实现
    public static int Catalan(int n) {

        if (n == 1)
            return 1;

        return (4 * n - 2) * Catalan(n - 1) / (n + 1) ;
    }
}

 

posted @ 2016-12-08 15:34  dreamerjdw  阅读(143)  评论(0编辑  收藏  举报