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): 9701    Accepted Submission(s): 5210

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

Recommend

We have carefully selected several similar problems for you:  1133 1022 1131 1134 2067

题意:给你n辆火车,编号从1到n,问你有多少种出站的可能序列。

思路:火车进出站问题,首先考虑卡特兰数(不要忘了高精哦)

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 110
using namespace std;
int n,len,sum,tmp,a[N][N],b[N];
int catelan()
{
    len=1;a[1][0]=b[1]=1;
    for(int i=2;i<110;i++)
    {
        for(int j=0;j<len;j++)
         a[i][j]=a[i-1][j]*(4*i-2);
        sum=0;
        for(int j=0;j<len;j++)
        {
            tmp=sum+a[i][j];
            a[i][j]=tmp%10;
            sum=tmp/10;
        }
        while(sum)
        {
            a[i][len++]=sum%10;
            sum/=10;
        }
        for(int j=len-1;j>=0;j--)
        {
            tmp=sum*10+a[i][j];
            a[i][j]=tmp/(i+1);
            sum=tmp%(i+1);
        }
        while(!a[i][len-1])
         len--;
        b[i]=len;
    }
}
int main()
{
    catelan();
    while(~scanf("%d",&n))
    {
        for(int i=b[n]-1;i>=0;i--)
         printf("%d",a[n][i]);
        printf("\n");
    }
}
时间: 2024-08-02 10:56:36

HDU——1023 Train Problem II的相关文章

Train Problem II 卡特兰裸题(入门题)

Train Problem II  题目大意:给你一个数n,表示有n辆火车,编号从1到n,从远方驶过来,问你有多少种出站的可能. 解题思路:模拟栈的问题而已.  卡特兰问题. 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 5 public class Main 6 { 7 static int MS=101; 8 public static void main(String[] args) 9 { 10 Sca

C - Train Problem II——(HDU 1023 Catalan 数)

传送门 Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7616    Accepted Submission(s): 4101 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train

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): 6454    Accepted Submission(s): 3514 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat

hdoj 1023 Train Problem II 【卡特兰】+【高精度】

题意:询问有多少种进站出站的顺序. 经典卡特兰.我对卡特兰目前的认识就是有n个1和n个-1,组成一个为2n的数列的方式有多少种.这就跟火车进站出站类似, 至于具体的卡特兰数的介绍,百度解释的很详细. 代码1(c语言): /* h(n) = h(n-1)*(4*n-2)/(n+1); */ #include <stdio.h> #include <string.h> #define M 110 int s[M][M] = {0}, b[M]; void init(){ s[1][0]

hdoj 1023 Train Problem II 【卡特兰数】

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6928    Accepted Submission(s): 3750 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat

ACM-卡特兰数之Train Problem II——hdu1023

***************************************转载请注明出处:http://blog.csdn.net/lttree*************************************** Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5589    Accept

HDU 1002 A + B Problem II(两个大数相加)

详细题目点击:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20)

HDU:A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 204863    Accepted Submission(s): 39378 Problem Description I have a very simple problem for you. Given two integers A and B, yo

抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. 1 while (a) //将每位数字取出来,取完为止 2 { 3 num1[i]=a%10; //将每一个各位取出存在数组里面,实现了将数字反转 4 i++; //数组的变化 5 a/=10; 6 } 趁热打铁 例题:hdu 4554 叛逆的小明 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid