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. django定时任务python调度框架APScheduler使用详解

    # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a ...

  2. Miller-Rabin大素数测试模板

    根据费马小定理: 对于素数n,a(0<a<n),a^(n-1)=1(mod n) 如果对于一个<n的正整数a,a^(n-1)!=1(mod n),则n必不是素数. 然后就可以随机生成 ...

  3. 串 2016Vijos省选集训 day3[AC自动机]

    1.串(string.c/.cpp/.pas) 限时1s,内存限制256MB,20个测试点 [题目描述] 兔子们在玩字符串的游戏.首先,它们拿出了一个字符串集合S,然后它们定义一个字符串为“好”的,当 ...

  4. web.xml配置中的log4jRefreshInterval

    采用spring框架的项目如何使用log4j在spring中使用log4j,有些方便的地方, 1.动态的改变记录级别和策略,即修改log4j.properties,不需要重启web应用,这需要在web ...

  5. maven filters 和 resource

    1 filter 1.1 用途 对多个配置文件进行选择. 1.2 选择的依据 1.3 使用的方式 第一,在<resource>标签下面加<filtering>标签,并且< ...

  6. [note]可持久化Trie

    可持久化Trie 参考可持久化线段树的思想,修改的时候先直接复制,再对需要修改的点新建节点 可持久化Trie也是同样的做法,假设现在需要在原本Trie的基础上插入一个字符串 先把上个Trie的对应节点 ...

  7. [转]How Hash Algorithms Work

    来看看SHA-1到底是如何工作的 http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software

  8. 怎样将lua移植到arm平台的linux内核

    将脚本移植到内核是一件非常酷的事情,lua已经被移植到NetBSD的内核中,也有一个叫lunatik的项目把lua移植到了linux内核.仅仅可惜仅仅支持x86.不支持arm,在网上搜索了下,没有找到 ...

  9. 【足迹C++primer】32、定制操作_1

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/cutter_point/article/details/32066151 定制操作 向算法传递函数 ...

  10. C调用Lua中的函数解析table

    Passing Tables to Lua Functions A use case that happens often is the passing of tables to and from L ...