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. 智能家居DIY-空气质量检测篇-获取空气污染指数

    前言 话说楼主终于升级当爸了,宝宝现在5个月了,宝宝出生的时候是冬天,正是魔都空气污染严重的时候,当时就想搞个自动开启空气净化器,由于种种原因一直没有时间搞,最近终于闲下来了这个事情终于提上议程了,现 ...

  2. 超轻量级、高性能C日志库--EasyLogger

    [ 声明:版权全部,欢迎转载.请勿用于商业用途. 联系信箱:armink.ztl@gmail.com] EasyLogger 1. 介绍 EasyLogger 是一款超轻量级(ROM<1.6K, ...

  3. python网络爬虫之scrapy 调试以及爬取网页

    Shell调试: 进入项目所在目录,scrapy shell “网址” 如下例中的: scrapy shell http://www.w3school.com.cn/xml/xml_syntax.as ...

  4. Office 2013“永久激活信息”备份

    Office 2013“永久激活信息”备份还原简明教程及成功恢复的注意事项Office 2013永久激活后及时备份激活信息可以保证重装后快速激活.网上也有流行的各种备份工具,虽然操作简单,但是如果不理 ...

  5. CentOS iSCSI服务器搭建------Initiator篇

    服务器信息: [root@initiator ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@initiator ~]# un ...

  6. iview 表单相关

    view表单验证的步骤: 第一步:给 Form 设置属性 rules :rules第二步:同时给需要验证的每个 FormItem 设置属性 prop 指向对应字段即可 prop=”“第三步:注意:Fo ...

  7. 在 CentOS7最小化 下的编译安装:Nginx 1.5.2 + PHP 5.5.7 + MySQL 5.6.10

    1.安装Nginx: 安装包目录 mkdir -p /Data/tgzcd /Data/tgz 安装编译依赖 yum install wget yum install pcre yum install ...

  8. elk示例-精简版

    作者:Danbo 2016-03-09 1.Grok正则捕获 input {stdin{}} filter { grok { match => { "message" =&g ...

  9. form memory cache、form disk cache与304

    200 from memory cache 不访问服务器,直接读缓存,从内存中读取缓存.此时的数据时缓存到内存中的,当kill进程后,数据将不存在200 from disk cache 不访问服务器, ...

  10. AdobeFlashPlayer.资料

    1.chrome 设置 chrome-->设置-->高级-->内容设置-->Flash 2. 3. 4. 5.