您的位置:首页 > 编程语言 > Java开发

HDOJ Train Problem II(Java)

2014-06-04 11:46 260 查看


[title3]Train Problem II[/title3]

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5578 Accepted Submission(s): 3016



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, you should output how many ways that all the trains can get out of the railway.

[align=left]Sample Input[/align]

1
2
3
10


[align=left]Sample Output[/align]

1
2
5
16796

Hint
The 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)种)2. 一个(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列?3. 有2n个人排成一行进入剧场。入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票,问有多少中方法使得只要有10元的人买票,售票处就有5元的钞票找零?(将持5元者到达视作将5元入栈,持10元者到达视作使栈中某5元出)	4. 在一个凸多边形中,通过若干条互不相交的对角线,把这个多边形划分成了若干个三角形。任务是键盘上输入凸多边形的边数n,求不同划分的方案数f(n)。比如当n=6时,f(6)=14。	5. 一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?	6. 在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数?	7. 给定N个节点,能构成多少种不同的二叉树
,它的功能还蛮强大啊!!!
最后贴上AC代码:[code]
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
);
}

}

}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: