卡特兰数:

Catalan数

  原理:

  令h(1)=1,h(0)=1,catalan数满足递归式:

  h(n)= h(1)*h(n-1) + h(2)*h(n-2) + ... + h(n-1)h(1) (其中n>=2)

  另类递归式:

  h(n)=((4*n-2)/(n+1))*h(n-1);

  该递推关系的解为:

  h(n+1)=C(2n,n)/(n+1) (n=1,2,3,...)

最典型的四类应用:(实质上却都一样,无非是递归等式的应用,就看你能不能分解问题写出递归式了)

  1.括号化问题。

  矩阵链乘: P=a1×a2×a3×……×an,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?(h(n)种)

  2.出栈次序问题。

  一个栈(无穷大)的进栈序列为1,2,3,..n,有多少个不同的出栈序列?

  类似:有2n个人排成一行进入剧场。入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票,问有多少中方法使得只要有10元的人买票,售票处就有5元的     钞票找零?(将持5元者到达视作将5元入栈,持10元者到达视作使栈中某5元出栈)

  3.将多边行划分为三角形问题。

  将一个凸多边形区域分成三角形区域的方法数?

  类似:一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她

  从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?

  类似:在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数?

  4.给顶节点组成二叉树的问题。

  给定N个节点,能构成多少种不同的二叉树?

  (能构成h(N)个)

Catalan数的解法

Catalan数的组合公式为 Cn=C(2n,n) / (n+1);

此数的递归公式为 h(n ) = h(n-1)*(4*n-2) / (n+1)

题目:

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. 

InputThe 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. 
OutputFor 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.

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int a[][];
void ktl()
{
a[][]=;
a[][]=;
a[][]=;
a[][]=;
int yu,temp,length=; //length 表示当前数字的长度
for (int i=;i<=;i++)
{
yu=;
for (int j=;j<=length;j++)
{
temp=(a[i-][j]*(*i-))+yu;
a[i][j]=temp%;
yu=temp/;
}
while (yu!=)
{
a[i][++length]=yu%;
yu/=;
}
for (int j=length;j>=;j--)
{
temp=a[i][j]+yu*;
a[i][j]=temp/(i+);
yu=temp%(i+);
}
while (!a[i][length])
length--;
a[i][]=length;
}
}
int main()
{
ktl();
int n;
while (scanf("%d",&n)==)
{
for (int i=a[n][];i>=;i--)
printf("%d",a[n][i]);
printf("\n");
}
return ;
}

Train Problem II (卡特兰数+大数问题)的更多相关文章

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

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

  2. C - Train Problem II——卡特兰数

    题目链接_HDU-1023 题目 As we all know the Train Problem I, the boss of the Ignatius Train Station want to ...

  3. HDU 1023 Train Problem II (卡特兰数,经典)

    题意: 给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路: 卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. #inclu ...

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

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

  5. HDOJ 1023 Train Problem II 卡特兰数

    火车进站出站的问题满足卡特兰数...卡特兰数的相关知识如下: 卡特兰数又称卡塔兰数,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名. ...

  6. Train Problem II(卡特兰数+大数乘除)

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

  7. HDU 1023 Train Problem II (大数卡特兰数)

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

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

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

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

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

  10. HDU_1023_Train Problem II_卡特兰数

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

随机推荐

  1. 计量经济与时间序列_自协方差(AutoCovariance)算法解析(Python)

    1 样本的自协方差函数的通式如下: 2 其实,后面要计算的自相关函数也可以用自协方差来表示: # @author: "Thomas.Shih" # @date: 2018/3/5 ...

  2. mysql查看变量

    在MySQL客户端执行如下命令查看MySQL的数据存放位置: show global variables like "%datadir%"; 查看端口号 show global v ...

  3. 分布式ID的简单总结

    来源:郴州网站优化 简单总结一下流行的分布式id的实现方法 雪花算法 snowflake是twitter开源的分布式ID生成算法. 核心思想是:分布式ID固定是一个long型的数字,一个long型占8 ...

  4. TPO9-2Reflection in Teaching

    Teachers, it is thought, benefit from the practice of reflection, the conscious act of thinking deep ...

  5. uboot 编译

    . 解包u-boot源码包(u-boot-2016.07) . 配置交叉编译器 根据内核编译里的步骤配置 . 编译uboot yum install ncurses* // ncurses是个终端的图 ...

  6. GB35658较796新增检测项部标平台

    GB35658较796新增检测项部标平台总共有113项,总结归类如下:1    报表导出    支持excel格式的报表导出    对查询.统计报表提供excel格式的报表导出    必选:    2 ...

  7. 基础篇八:log配置

    第一:首选查看有哪些日志文件 cd /etc/nginx/ cat nginx.conf cd /var/log/nginx/

  8. 005.前端开发知识,前端基础CSS(2020-01-14)

    一.CSS权重 权重是可以叠加的,事例如下: div ul li ------> 0,0,0,3 .nav ul li ------> 0,0,1,2 a:hover -----—> ...

  9. 68.26-95.44-99.74 rule|empirical rule

    6.3 Working with Normally Distributed Variables As illustrated in the previous example, the 68.26-95 ...

  10. Java接口和抽象类区别

    1.抽象类 [public] abstract class ClassName { abstract void fun(); } extends 包含抽象方法的类称为抽象类,但并不意味着抽象类中只能有 ...