Train Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5578 Accepted Submission(s): 3016
Problem 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 16796HintThe result will be very large, so you may not process it by 32-bit integers.问题:本题是火车的调度问题,题意大致是N列火车以严格递增的次序进入轨道(相当于进栈),问火车行驶出轨道(也就是出栈)总共有多少种方案?分析:本题主要是使用一个叫卡特兰数列的递推格式就可以求解,但是问题的解答方案可能有很多种,这就要用到大数,还好java中提供了叫BigInteger类可以处理高精度的问题;下面首先讲解什么是卡特兰数列,以下是百度百科里的讲解:卡特兰数又称卡塔兰数,是 组合数学 中一个常出现在各种计数问题中出现的 数列 。由以 比利时 的数学家欧仁·查理·卡塔兰 (1814–1894)命名。它的递推格式为:h(n)=h(n-1)*(4*n-2)/(n+1),其中h(0)=1,h(1)=1;另外还有其他的递推格式形式:h(n)=C(2n,n)/(n+1) (n=0,1,2,...)或h(n)=c(2n,n)-c(2n,n+1)(n=0,1,2,...)。其应用有:1. 矩阵链乘: P=a1×a2×a3×……×an,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?(h(n-1)种)3. 有2n个人排成一行进入剧场。入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票,问有多少中方法使得只要有10元的人买票,售票处就有5元的钞票找零?(将持5元者到达视作将5元入栈,持10元者到达视作使栈中某5元出栈)5. 一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?,它的功能还蛮强大啊!!!最后贴上AC代码:import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger[] bigNumber = new BigInteger[105]; bigNumber[0] = BigInteger.ONE ; bigNumber[1] = BigInteger.ONE; for(int i=2; i<=100; i++){ BigInteger multiply = new BigInteger(String.valueOf(4*i-2)); BigInteger divide = new BigInteger(String.valueOf(i+1)); bigNumber[i] = bigNumber[i-1].multiply(multiply).divide(divide); } Scanner in = new Scanner(System.in); while(in.hasNextInt()){ int n = in.nextInt(); System.out.println(bigNumber[n]); } } }