首页 > 其他 > 详细

HDU——1023 Train Problem II

时间:2017-08-10 09:42:32      阅读:186      评论:0      收藏:0      [点我收藏+]

                     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");
    }
}

 

HDU——1023 Train Problem II

原文:http://www.cnblogs.com/z360/p/7337345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!