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得来,大意就是有一个火车序列,要进入一个栈或出去,问输出顺序的种数。

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

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

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

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

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

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

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[][];
for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
p[i][j] = new BigInteger("");
for (int i = ; i < ; ++i)
p[i][] = new BigInteger("");
for (int j = ; j <= ; ++j)
for (int i = ; i <= ; ++i)
if (i > )
p[i][j] = p[i-][j].add(p[i+][j-]);
else
p[i][j] = p[i+][j-];
int n;
while (input.hasNext())
{
n = input.nextInt();
System.out.println(p[][n]);
}
}
}

ACM学习历程—HDU1023 Train Problem II(递推 && 大数)的更多相关文章

  1. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  2. ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)

    Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...

  3. HDU-1023 Train Problem II 卡特兰数(结合高精度乘除)

    题目链接:https://cn.vjudge.net/problem/HDU-1023 题意 卡特兰数的应用之一 求一个长度为n的序列通过栈后的结果序列有几种 思路 一开始不知道什么是卡特兰数,猜测是 ...

  4. ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

  5. HDU1023 Train Problem II【Catalan数】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1023 题目大意: 一列N节的火车以严格的顺序到一个站里.问出来的时候有多少种顺序. 解题思路: 典型 ...

  6. (母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  8. hdu 1023 Train Problem II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...

  9. Train Problem II(卡特兰数 组合数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. Centos 初始化服务器防火墙没有启动找不到/etc/sysconfig/iptables

    个人博客:https://blog.sharedata.info/ 具体步骤:添加规则然后重启防火墙自动生成防火墙文件1.iptables -P OUTPUT ACCEPT #添加出规则2.servi ...

  2. mysql时间相减的问题

    MySQL中时间不能直接相减,如果日.分.时不同,相减结果是错误的 mysql> select t1,t2,t2-t1 from mytest;   +--------------------- ...

  3. C# 6.0 (C# vNext) 新功能之:Null-Conditional Operator(转)

    Null-Conditional Operator 也叫 Null propagating operator 也叫 Safe Navigation Operator 看名字,应该就有点概念了.如果还不 ...

  4. DNN 数据的表示

  5. jquery点击一组按钮中的一个,跳转至对应页面处理策略。(如点击订单列表中的一个订单,跳转至该订单的详情)

    将改组按钮的数据设置一个相同的属性(如class),然后每个按钮设置不同的id 当用户点击属性为class的按钮,根据id属性来判断点击的是哪个按钮,然后进行相关操作. 代码示例: <scrip ...

  6. Java基础 - 变量转换

    在java中变量转发分为两种,隐式转换和强制转换 隐式转换: byte a = 10; int b = 20; byte c = a + b; // 该方法会报错,转换过程中字节数只能从小变大,不能从 ...

  7. GTK+重拾--07 GTK+中的事件

    (一):写在前面 在这一个小节中,我们主要是学习GTK+2.0中最重要的部分.就是信号和事件.GTK+函数工具库是基于"事件"系统的.全部的GUI应用都是基于"事件&qu ...

  8. hdu2563——统计问题

    Problem Description 在一无限大的二维平面中,我们做例如以下如果: 1.  每次仅仅能移动一格. 2.  不能向后走(如果你的目的地是"向上",那么你能够向左走, ...

  9. C++学习笔记30,指针的引用(2)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/guang_jing/article/details/32910093 能够创建不论什么类型的引用,包 ...

  10. RLearning第2弹:创建数据集

    任何一门语言,数据类型和数据结构是最基础,也是最重要的,必须要学好!1.产生向量 a<-c(1,2,5,3,6,-2,4) b<-c("one","two&q ...