zoukankan      html  css  js  c++  java
  • Train Problem II (卡特兰数+大数问题)

    卡特兰数:

    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[110][100];
    void ktl()
    {
        a[1][0]=1;
        a[2][0]=1;
        a[1][1]=1;
        a[2][1]=2;
        int yu,temp,length=1;  //length 表示当前数字的长度
        for (int i=3;i<=100;i++)
        {
            yu=0;
            for (int j=1;j<=length;j++)
            {
                temp=(a[i-1][j]*(4*i-2))+yu;
                a[i][j]=temp%10;
                yu=temp/10;
            }
            while (yu!=0)
            {
                a[i][++length]=yu%10;
                yu/=10;
            }
            for (int j=length;j>=1;j--)
            {
                temp=a[i][j]+yu*10;
                a[i][j]=temp/(i+1);
                yu=temp%(i+1);
            }
            while (!a[i][length])
                length--;
                a[i][0]=length;
        }
    }
    int main()
    {
        ktl();
        int n;
        while (scanf("%d",&n)==1)
        {
            for (int i=a[n][0];i>=1;i--)
                printf("%d",a[n][i]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    SAP的PI日志查看工具
    微信小程序调用SAP发布的REST显示数据列表
    SAP发布REST/HTTP接口
    SAP的JSON没有双引号问题
    SAP扩展库位
    函数使用十一:BAPI_BANK_CREATE
    竟然有人在群里谈交钱培训PI。。。。等哥哥有时间,断了你们的财路
    FPM十一:点击POPUP显示明细
    WDA基础十八:Select option配置
    SAP常见查询组合
  • 原文地址:https://www.cnblogs.com/lisijie/p/7260058.html
Copyright © 2011-2022 走看看