hdu1032 Train Problem II (卡特兰数)

题意:

给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能。    (题于文末)

知识点:

ps:百度百科的卡特兰数讲的不错,注意看其参考的博客。

卡特兰数(Catalan):前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670…

令h(0)=1,h(1)=1,catalan数满足递推式:

     h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)

另类递推式:

hdu1032 Train Problem II (卡特兰数)

递推关系的解为:

hdu1032 Train Problem II (卡特兰数)

递推关系的另类解为:

hdu1032 Train Problem II (卡特兰数)

对于在2n位的2进制中,有n个0,其余为1,且1的累计数>=0的累计数,二进制数有hdu1032 Train Problem II (卡特兰数)

对于在n位的2进制中,有m个0,其余为1的catalan数为:hdu1032 Train Problem II (卡特兰数)

理解:catalan数的理解

应用:

1.出栈次序: 一个栈(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列?          h(n)种。

2.给定节点组成二叉树:给定n个节点,能构成多少种不同的二叉树?    h(n)种。

3.括号化:矩阵连乘,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,有几种括号化的方案?  h(n-1)种。

4.凸多边形三角划分:在一个凸n边形中,通过若干条互不相交的对角线,有多少种方法把这个多边形划分成若干个三角形?         h(n-2)种。

/**/

题解:

此题为应用1,运用公式   hdu1032 Train Problem II (卡特兰数)

catalan数计算一般都涉及大数运算,java写起来方便。

import java.util.Scanner;
import java.math.BigInteger;
import java.io.*; public class Main{
public static void main(String[] args){
int n;
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
n=sc.nextInt();
BigInteger ans=BigInteger.valueOf(1);
for(int i=1;i<=n;i++){
ans=ans.multiply(BigInteger.valueOf(4*i-2));
ans=ans.divide(BigInteger.valueOf(i+1));
}
System.out.println(ans);
}
}
}

Train Problem II

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.

Input

The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.

Output

For each test case, you should output how many ways that all the trains can get out of the railway.

Sample Input


1
2
3
10

Sample Output


1
2
5
16796

Hint

 The result will be very large, so you may not process it by 32-bit integers. 
上一篇:BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】


下一篇:myeclipse2017破解失败解决办法