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;
    }
  • 相关阅读:
    移动性能测试 | 持续集成中的 Android 稳定性测试
    iOS 测试 | iOS 自动化性能采集
    Google 测试总监聊如何经营成功的测试职业生涯
    浅谈一下可扩展性网站架构设计
    一条SQL执行慢的原因有哪些
    为什么在做微服务设计的时候需要DDD?
    是时候拥抱.NET CORE了
    MySql多表查询优化
    九种高性能可用高并发的技术架构
    HTTP协议总结
  • 原文地址:https://www.cnblogs.com/jackge/p/3170612.html
Copyright © 2011-2022 走看看