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 }
  • 相关阅读:
    归档拷贝LogMiner 和 archived redo log,分析归档日志
    宋体测试液晶屏测试小程序
    宋体方法Unity3d通过苹果和google两种方法加入广告
    代码确认小米官方在线预定——对代码实现的分析
    分区启动Grub2配置详解
    临时表判断sqlite 判断表 或 临时表 是否存在
    解释升级JDeveloper&ADF的版本
    引用字符shell中的引用java教程
    枚举生成MVC3: Bind Enum To DropDownList ?java教程
    能力学习为什么你应聘不上或试用期被开?
  • 原文地址:https://www.cnblogs.com/kirai/p/4753147.html
Copyright © 2011-2022 走看看