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): 6852 Accepted Submission(s): 3708

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 1130 1131 1134 2067

卡特兰数的递推公式为: h(n)=  h(n-1)*[ (4*n-2)/ (n+1)]

#include<stdio.h>
#include<string.h>
const int MAXN=200;
int catalan[105][MAXN];
int temp[MAXN];
void create(){
	memset(catalan,0,sizeof(catalan));
	catalan[1][0] = 1;
	int i,j,res;
	for(i = 2; i <= 100; ++i) {
		int mid = 4*i-2;
		for(j = 0;j < MAXN;++j){
		    catalan[i][j] += catalan[i-1][j] * mid;
		    if(catalan[i][j]>=10){
		    	catalan[i][j+1] += catalan[i][j]/10;
		    	catalan[i][j] = catalan[i][j]%10;
		    }
		}
		memset(temp,0,sizeof(temp));
		mid=i+1;
		res=0;
		for(j=MAXN-1;j>=0;--j){
			temp[j] = (res*10 + catalan[i][j])/mid;
			res = (10*res + catalan[i][j])%mid;
		}
		for(j=0; j<MAXN; ++j){
			catalan[i][j] = temp[j];
		}
	}
}
int main(){
	create();
	int n;
	while(~scanf("%d",&n)){
		int i=MAXN-1;
		while(!catalan[n][i]) --i;
		for(;i>=0;--i){
			printf("%d",catalan[n][i]);
		}
		printf("\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-04 09:42:34

hdoj-1023-Train Problem II【卡特兰数】的相关文章

HDU 1023 Train Problem II (卡特兰数,经典)

题意:给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路:卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N=101; 4 vector<string> vect; 5 void _mult(string num1, string num2, string &result )

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

HDOJ 1023 Train Problem II (卡塔兰数)

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 se

hdu1023 Train Problem II(卡特兰数)

题目意思: http://acm.hdu.edu.cn/showproblem.php?pid=1023 求出第n个卡特兰数,n<1000. 题目分析: 很明显c(n)将很大,我们可以用大数模板,也可以用java中的大整数类,这里用到了java,将java在处理大数的时候还是很有优势的. AC代码: /** * java实现卡特兰数 * 前几项:1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786,208012- * 公式 C(n)=C(2n,n

hdu acm 1023 Train Problem II -&gt;卡特兰(Catalan大数)

#include<iostream> using namespace std; struct Node //节点 { int num[105]; int len; //数的长度 } a[105]; void CalCatalen() //卡特兰数计算 { int i,j,len,c,t; //len长度,c进位 a[1].num[0]=a[1].len=1; len=1; for(i=2;i<=100;i++) { for(j=0;j<len;j++) //乘法,分子部分 a[i]

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

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

1023 Train Problem II(卡特兰数)

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 se

hdu 1023 Train Problem II

卡特兰数的应用 :卡特兰数参考kuangbing总结http://www.cnblogs.com/kuangbin/archive/2012/03/21/2410516.html. 大数的运算 hdu题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1023 1 #include<algorithm> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<iostream