zoukankan      html  css  js  c++  java
  • 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]);
            }
        }
    }
  • 相关阅读:
    Jquery中的this指向的是哪个对象?
    需要重新编辑
    关于CSS选择器优先级无冲突样式设置的展示
    在 CSS 中,width 和 height 指的是内容区域的宽度和高度
    关于正则表达式中分组的一些误解勘正以及String的replaceAll方法误解勘正
    关于informatica的Dynamic Lookup组件使用中遇到的一个问题的思考
    【转】Informatica Update 机制详解
    维度表和事实表的含义
    今天看IO流,复制word遇到的一个小问题
    小试下新博客,一个列传行的SQL
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4928508.html
Copyright © 2011-2022 走看看