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 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.

卡特兰数:百度百科

代码:

#include <stdio.h>

int cat[110][100] ;

int main()
{
	int n ;
	cat[0][0] = cat[1][0] = 1 ;
	cat[0][1] = cat[1][1] = 1 ;
	for(int i = 2 ; i <= 100 ; ++i)
	{
		for(int j = 1 ; j <= cat[i-1][0] ; ++j)
		{
			cat[i][j] = cat[i-1][j]*(4*i-2) ;
		}
		int temp = 0 , len = 1;
		for(len = 1 ; len <= cat[i-1][0] ; ++len)
		{
			cat[i][len] += temp ;
			temp = cat[i][len]/10 ;
			cat[i][len] %= 10 ;
		}
		while(temp != 0)
		{
			cat[i][len++] += temp%10 ;
			temp /= 10 ;
		}
		for(int j = len-1 , r = 0; j > 0 ; --j)
		{
			temp = r*10+cat[i][j] ;
			r = temp%(i+1) ;
			cat[i][j] = temp/(i+1) ;
		}
		while(cat[i][len] == 0)
		{
			--len ;
		}
		cat[i][0] = len ;
	}
	while(~scanf("%d",&n))
	{
		for(int i = cat[n][0] ; i > 0 ; --i)
			printf("%d",cat[n][i]) ;
		puts("") ;
	}
	return 0 ;
}

与君共勉

时间: 2024-10-25 16:23:28

hdu 1023 Train Problem II 这题运用到大数相乘+大数相除+卡特兰数的相关文章

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

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 )

HDU 1023 Train Problem II 大数打表Catalan数

一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n,n) / (n+1); 递推公式 C(n ) = C(n-1)*(4*n-2) / (n+1) 高精度乘以一个整数和高精度除以一个整数的知识.这样还是使用整数数组比較好计算,假设使用string那么就不太好计算了,由于整数也可能是多位的. const int MAX_N = 101; short

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

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