• HDU 1023 Traning Problem (2) 高精度卡特兰数


    Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

     Status

    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. 
             
     

    Source

    求高精度的卡特兰数。

    1.java代码,套公式就可以了。

    import java.io.*;  
    import java.util.*;  
    import java.math.BigInteger;  
      
      
    public class Main  
    {  
        public static void main(String args[])  
        {         
            BigInteger[] a = new BigInteger[101];  
            a[0] = BigInteger.ZERO;  
            a[1] = BigInteger.valueOf(1);  
            for(int i = 2; i <= 100; ++i)  
                a[i] = a[i - 1].multiply(BigInteger.valueOf(4 * i - 2)).divide(BigInteger.valueOf(i+1));  
                Scanner in = new Scanner(System.in);  
                int n;  
                while(in.hasNext())  
                {  
                    n = in.nextInt();  
                    System.out.println(a[n]);  
                }  
        }  
    }  

     2.C++代码,kuangbin模板

    //h( n ) = ( ( 4*n-2 )/( n+1 )*h( n-1 ) );
    
    
    #include<stdio.h>
    
    //*******************************
    //打表卡特兰数
    //第 n个 卡特兰数存在a[n]中,a[n][0]表示长度;
    //注意数是倒着存的,个位是 a[n][1] 输出时注意倒过来。 
    //*********************************
    int a[105][100];
    void ktl()
    {
        int i,j,yu,len;
        a[2][0]=1;
        a[2][1]=2;
        a[1][0]=1;
        a[1][1]=1;
        len=1;
        for(i=3;i<101;i++)
        {
            yu=0;
            for(j=1;j<=len;j++)
            {
                int t=(a[i-1][j])*(4*i-2)+yu;
                yu=t/10;
                a[i][j]=t%10;
            }    
            while(yu)
            {
                a[i][++len]=yu%10;
                yu/=10;
            }
            for(j=len;j>=1;j--)
            {
                int t=a[i][j]+yu*10;
                a[i][j]=t/(i+1);
                yu = t%(i+1);
            }        
            while(!a[i][len])
            {
                len--;
            }    
            a[i][0]=len;
        }    
        
    }    
    int main()
    {
        ktl();
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=a[n][0];i>0;i--)
            {
                printf("%d",a[n][i]);
            }    
            puts("");
        }    
        return 0;
    }

    3.C++代码

    #include <iostream>  
    #include <stdio.h>  
    #include <cmath>  
    using namespace std;  
      
    int a[105][105];    //大数卡特兰数  
    int b[105];         //卡特兰数的长度  
      
    void catalan()  //求卡特兰数  
    {  
        int i, j, len, carry, temp;  
        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-1)+2);  
            carry = 0;  
            for(j = 0; j < len; j++)    //处理相乘结果  
            {  
                temp = a[i][j] + carry;  
                a[i][j] = temp % 10;  
                carry = temp / 10;  
            }  
            while(carry)    //进位处理  
            {  
                a[i][len++] = carry % 10;  
                carry /= 10;  
            }  
            carry = 0;  
            for(j = len-1; j >= 0; j--) //除法  
            {  
                temp = carry*10 + a[i][j];  
                a[i][j] = temp/(i+1);  
                carry = temp%(i+1);  
            }  
            while(!a[i][len-1])     //高位零处理  
                len --;  
            b[i] = len;  
        }  
    }  
      
    int main()  
    {  
        int i, n;  
        catalan();  
        while(scanf("%d", &n) != EOF)  
        {  
            for(i = b[n]-1; i>=0; i--)  
            {  
                printf("%d", a[n][i]);  
            }  
            printf("
    ");  
        }  
      
        return 0;  
    }  
  • 相关阅读:
    zech的神秘题库(武汉理工夜莺杯)
    回归第六题
    同余方程
    牛牛选路径(牛客)
    回归第三题
    区间dp复习提高专题
    乘法逆元(线性递推)
    回归第八题
    JAVA启动参数大全之二:非标准参数(转)
    (转)Spring Security 3.1 自定义实例之登陆
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5343085.html
Copyright © 2020-2023  润新知