zoukankan      html  css  js  c++  java
  • 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): 4358    Accepted Submission(s): 2391


    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
     

     这题的意思就是和那个经典的出栈次序问题一模一样,就是卡特兰

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int a[110][110];    //大数卡特兰数
    int b[110];         //卡特兰数的长度 
    
    void Catalan(){     //求卡特兰数
        int i,j,len,carry,tmp;
        a[1][0]=b[1]=1;
        len=1;
        for(i=2;i<=100;i++){
            for(j=0;j<len;j++)      //乘法 
                a[i][j]=a[i-1][j]*(4*i-2);
            carry=0;
            for(j=0;j<len;j++){     //处理相乘结果
                tmp=carry+a[i][j];
                a[i][j]=tmp%10;
                carry=tmp/10;
            }
            while(carry){       //进位处理 
                a[i][len++]=carry%10;
                carry/=10;
            }
            //carry=0;
            for(j=len-1;j>=0;j--){  //除法 
                tmp=carry*10+a[i][j];
                a[i][j]=tmp/(i+1);
                carry=tmp%(i+1);
            }   
            while(!a[i][len-1])     //高位零处理
                len--;
            b[i]=len;
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n;
        Catalan();
        while(~scanf("%d",&n)){
            for(int i=b[n]-1;i>=0;i--)
                printf("%d",a[n][i]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    Java开源内容管理CMS系统J4CMS支持静态化直接ftp上传到你的空间了
    JAVA数组的定义及用法
    从本地上传整个目录到hdfs的java程序
    图片轮显效果大全
    Windows 自己主动关机命令 shuntdown
    Android源码是这样搞到的(图解)
    JSONObject与JSONArray的使用
    教你用笔记本破解无线路由器password
    tomcat配置文件server.xml具体解释
    SQLite的SQL语法
  • 原文地址:https://www.cnblogs.com/jackge/p/3170612.html
Copyright © 2011-2022 走看看