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]);
            }
        }
    }
  • 相关阅读:
    opengl 4.5离线文档下载
    c++下利用URLOpenStream下载
    实时阴影渲染(三):软阴影深度校正
    实时阴影渲染(二):软阴影
    实时阴影渲染(一):PSSM平行分割阴影图
    Ogre材质shader模版
    本地springboot项目连接本地mysql报错。com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
    Caused by: java.lang.IllegalStateException: AnnotationAwareAspectJAutoProxyCreator is only available on Java 1.5 and higher
    关于布隆过滤器
    CSS系列------选择器和选择器的优先级
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4928508.html
Copyright © 2011-2022 走看看