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;
    }
  • 相关阅读:
    elasticsearch 基础
    docker 安装ElasticSearch的中文分词器IK
    Netty的编解码,粘包拆包,心跳检测机制
    Spring IOC 常用的注解
    @JsonView的使用
    lombok的使用以及其中的坑
    Zuul的高级使用
    SpringBoot 整合 ActiveMq
    转:Maven <resource>标签
    码农经常读错的单词
  • 原文地址:https://www.cnblogs.com/jackge/p/3170612.html
Copyright © 2011-2022 走看看