• ACM学习历程—HDU1023 Train Problem II(递推 && 大数)


    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. 

    题目大意要根据问题I得来,大意就是有一个火车序列,要进入一个栈或出去,问输出顺序的种数。

    首先我试了一下对nn-1的关系进行讨论,发现对其栈的出入过程还是有依赖的,所以需要对中间状态进行讨论。

    于是设置状态p(i, j, k)表示为进栈的火车有i个,栈中的火车有j个,出栈的火车有k个。

    然后p表示的是当前状态下最终能生成的种类数。

    其实发现k那一维是多余的,因为已经出栈的火车序列已经固定,只有后面栈中和未进栈的火车会影响序列的顺序。而且无论k为几,p的结果只取决于ij

    状态出来了,于是可以讨论状态间的关系。

    显然操作只有进栈和出栈两种。

    p[i][j] = p[i-1][j]+p[i+1][j-1];

    不过需要考虑j=0的情况。

    据说这个是传说中的卡特兰数。

    还有就是数据规模很大,需要用大数,此处采用了 java

    代码:

    import java.math.BigInteger;
    import java.util.Scanner;
    
    
    public class Main
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            BigInteger p[][] = new BigInteger[105][105];
            for (int i = 0; i < 105; ++i)
                for (int j = 0; j < 105; ++j)
                    p[i][j] = new BigInteger("0");
            for (int i = 1; i < 105; ++i)
                p[i][0] = new BigInteger("1");
            for (int j = 1; j <= 100; ++j)
                for (int i = 0; i <= 100; ++i)
                    if (i > 0)
                        p[i][j] = p[i-1][j].add(p[i+1][j-1]);
                    else
                        p[i][j] = p[i+1][j-1];
            int n;
            while (input.hasNext())
            {
                n = input.nextInt();
                System.out.println(p[0][n]);
            }
        }
    }
  • 相关阅读:
    11个Linux基础面试问题
    OSI模型
    戴文的Linux内核专题:10配置内核(6)
    面向对象实验四(输入输出流)
    计算机程序的思维逻辑 (2)
    计算机程序的思维逻辑 (1)
    java基础3.0:Java常用API
    java基础2.0:Object、Class、克隆、异常编程
    java基础1.0::Java面向对象、面向对象封装、抽象类、接口、static、final
    Ajax工作原理(转)
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4928508.html
Copyright © 2020-2023  润新知