HDU-1023 Train Problem II 出栈序列问题

    题目:HDU-1023

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023

    题目:

Train Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7427    Accepted Submission(s): 4008


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 16796
Hint
The result will be very large, so you may not process it by 32-bit integers.
 
    先上代码,用的是Java大数据:

import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    BigInteger a=new BigInteger("1");
    BigInteger b=new BigInteger("2");
    BigInteger c=new BigInteger("4");
    BigInteger d=new BigInteger("1");
    BigInteger ans[]=new BigInteger[105];
    ans[1]=a;
    for(int i=2;i<=100;i++){
        d=d.add(a);
        ans[i]=ans[i-1].multiply(c.multiply(d).subtract(b)).divide(d.add(a));
    }
    while(input.hasNext()){
    int t=input.nextInt();
    System.out.println(ans[t]);
    }
    }
}

    嗯哼,经过本人 并不是很严密的推论,记住,很(不)严密,看看就行了。

    当入栈元素只有1个的时候,毫无疑问出栈序列只有一种。

    当入栈元素为2个的时候,比如1,2,出栈序列就有1,2和2,1两种,如果这个不理解的话回头看看栈的定义吧。

    当入栈元素为3个的时候,1,2,3,则分别有1开头,2开头,3开头的序列,分别是,1*f(n-1)+f(n-1)*1,然后还剩下中间以2开头的,然后就可以转化为定义式来看待序列总数。

    大概就是这样,具体的也搞不太懂,以后再深入研究吧。


你可能感兴趣的:(HDU,1023)