zoukankan      html  css  js  c++  java
  • [HDOJ1023]Train Problem II

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023

     

    Train Problem II

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6917    Accepted Submission(s): 3742


    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.
     
     
    ( 卡特兰数可以表示一个栈有多少个不同的出栈序列)
    大数卡特兰数的裸题,关于卡特兰数的介绍在这里:http://www.cppblog.com/MiYu/archive/2010/08/07/122573.html
     
    代码:
     
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cmath>
     4 using namespace std;
     5 
     6 //Catalan
     7 int cat[105][105];    //大数卡特兰
     8 int clen[105];         //卡特兰数的长度
     9 int n;
    10 
    11 void catalan() {
    12     int i, j, len, carry, temp;
    13     cat[1][0] = clen[1] = 1;
    14     len = 1;
    15     for(i = 2; i <= 100; i++) {
    16         for(j = 0; j < len; j++)
    17             cat[i][j] = cat[i-1][j]*(4*(i-1)+2);
    18         carry = 0;
    19         for(j = 0; j < len; j++) {
    20             temp = cat[i][j] + carry;
    21             cat[i][j] = temp % 10;
    22             carry = temp / 10;
    23         }
    24         while(carry) {
    25             cat[i][len++] = carry % 10;
    26             carry /= 10;
    27         }
    28         carry = 0;
    29         for(j = len-1; j >= 0; j--) {
    30             temp = carry*10 + cat[i][j];
    31             cat[i][j] = temp/(i+1);
    32             carry = temp%(i+1);
    33         }
    34         while(!cat[i][len-1]) {
    35             len --;
    36         }
    37         clen[i] = len;
    38     }
    39 }
    40 
    41 void solve() {
    42     for(int i = clen[n] - 1; i >= 0; i--) {
    43         printf("%d", cat[n][i]);
    44     }
    45     printf("
    ");
    46 }
    47 
    48 int main() {
    49     catalan();
    50     while(scanf("%d", &n) != EOF) {
    51         solve();
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    rpm命令详解
    Linux基础提高_系统性能相关命令
    Day004_Linux基础命令之特殊符号与正则表达式通配符
    Linux基础_网站权限规划
    Day005_Linux基础之文件权限
    Day003_linux基础_系统启动过程及系统安装后优化
    win7旗舰版安装不了mysql问题-------win7系统版本选择问题的一点探索
    Java程序结构
    NCRE Java二级备考方案
    NCRE的JAVA二级考试大纲
  • 原文地址:https://www.cnblogs.com/kirai/p/4753147.html
Copyright © 2011-2022 走看看