Train Problem II HDU 1023 卡特兰数

Problem 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.

Author

Ignatius.L

出栈次序

一个栈(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列?[4-5]

常规分析

首先,我们设f(n)=序列个数为n的出栈序列种数。(我们假定,最后出栈的元素为k,显然,k取不同值时的情况是相互独立的,也就是求出每种k最后出栈的情况数后可用加法原则,由于k最后出栈,因此,在k入栈之前,比k小的值均出栈,此处情况有f(k-1)种,而之后比k大的值入栈,且都在k之前出栈,因此有f(n-k)种方式,由于比k小和比k大的值入栈出栈情况是相互独立的,此处可用乘法原则,f(n-k)*f(k-1)种,求和便是Catalan递归式。ps.author.陶百百)

首次出空之前第一个出栈的序数k将1~n的序列分成两个序列,其中一个是1~k-1,序列个数为k-1,另外一个是k+1~n,序列个数是n-k。

此时,我们若把k视为确定一个序数,那么根据乘法原理,f(n)的问题就等价于——序列个数为k-1的出栈序列种数乘以序列个数为n - k的出栈序列种数,即选择k这个序数的f(n)=f(k-1)×f(n-k)。而k可以选1到n,所以再根据加法原理,将k取不同值的序列种数相加,得到的总序列种数为:f(n)=f(0)f(n-1)+f(1)f(n-2)+……+f(n-1)f(0)。

看到此处,再看看卡特兰数的递推式,答案不言而喻,即为f(n)=h(n)= C(2n,n)/(n+1)= c(2n,n)-c(2n,n-1)(n=0,1,2,……)。

最后,令f(0)=1,f(1)=1。

非常规分析

对于每一个数来说,必须进栈一次、出栈一次。我们把进栈设为状态‘1’,出栈设为状态‘0’。n个数的所有状态对应n个1和n个0组成的2n位二进制数。由于等待入栈的操作数按照1‥n的顺序排列、入栈的操作数b大于等于出栈的操作数a(a≤b),因此输出序列的总数目=由左而右扫描由n个1和n个0组成的2n位二进制数,1的累计数不小于0的累计数的方案种数。

在2n位二进制数中填入n个1的方案数为c(2n,n),不填1的其余n位自动填0。从中减去不符合要求(由左而右扫描,0的累计数大于1的累计数)的方案数即为所求。

不符合要求的数的特征是由左而右扫描时,必然在某一奇数位2m+1位上首先出现m+1个0的累计数和m个1的累计数,此后的2(n-m)-1位上有n-m个 1和n-m-1个0。如若把后面这2(n-m)-1位上的0和1互换,使之成为n-m个0和n-m-1个1,结果得1个由n+1个0和n-1个1组成的2n位数,即一个不合要求的数对应于一个由n+1个0和n-1个1组成的排列。

反过来,任何一个由n+1个0和n-1个1组成的2n位二进制数,由于0的个数多2个,2n为偶数,故必在某一个奇数位上出现0的累计数超过1的累计数。同样在后面部分0和1互换,使之成为由n个0和n个1组成的2n位数,即n+1个0和n-1个1组成的2n位数必对应一个不符合要求的数。

因而不合要求的2n位数与n+1个0,n-1个1组成的排列一一对应。

显然,不符合要求的方案数为c(2n,n+1)。由此得出输出序列的总数目=c(2n,n)-c(2n,n+1)=c(2n,n)/(n+1)=h(n)。

类似问题 买票找零

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN  102
#define INF 1000000009
/*
给定递增顺序进入,问有多少种方式出栈
1 1
2: 1-2 2-1
3: 123
*/
int a[MAXN][MAXN];
void init()
{
    int i, j, yu, len;
    a[1][0] = 1; a[1][1] = 1;
    a[2][0] = 1; a[2][1] = 2;
    len = 1;
    for (i = 3; i < MAXN; i++)
    {
        yu = 0;
        for (j = 1; j <= len; j++)
        {
            int tmp = a[i - 1][j] * (4 * i - 2) + yu;
            yu = tmp / 10;
            a[i][j] = tmp % 10;
        }
        while (yu)
        {
            a[i][++len] = yu % 10;
            yu /= 10;
        }
        for (j = len; j > 0; j--)
        {
            int tmp = a[i][j] + yu*10;
            a[i][j] = tmp / (i + 1);
            yu = tmp % (i + 1);
        }
        while (!a[i][len])
            len--;
        a[i][0] = len;
    }
}
int main()
{
    init();
    int n;
    while (scanf("%d", &n) != EOF)
    {
        for (int i = a[n][0]; i > 0; i--)
            printf("%d", a[n][i]);
        printf("\n");
    }
}
时间: 2024-12-26 12:02:01

Train Problem II HDU 1023 卡特兰数的相关文章

A - Train Problem II ( 火车和卡特兰数 )

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. Eac

hdu 1023 卡特兰数+高精度

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem 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-increasi

HDOJ 1023 Train Problem II (卡塔兰数)

Problem 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 se

hdu 1023 卡特兰数

import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner in = new Scanner(System.in); BigInteger[] a = new BigInteger[105]; for(int i = 0;i < 105;i++){ a[i] = new BigInteger("1"

hdu 1023 Train Problem II 这题运用到大数相乘+大数相除+卡特兰数

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6454    Accepted Submission(s): 3514 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat

C - Train Problem II——(HDU 1023 Catalan 数)

传送门 Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7616    Accepted Submission(s): 4101 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train

hdoj 1023 Train Problem II 【卡特兰数】

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6928    Accepted Submission(s): 3750 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat

hdoj 1023 Train Problem II 【卡特兰】+【高精度】

题意:询问有多少种进站出站的顺序. 经典卡特兰.我对卡特兰目前的认识就是有n个1和n个-1,组成一个为2n的数列的方式有多少种.这就跟火车进站出站类似, 至于具体的卡特兰数的介绍,百度解释的很详细. 代码1(c语言): /* h(n) = h(n-1)*(4*n-2)/(n+1); */ #include <stdio.h> #include <string.h> #define M 110 int s[M][M] = {0}, b[M]; void init(){ s[1][0]

HDU——1023 Train Problem II

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9701    Accepted Submission(s): 5210 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat