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 catalan[MAX_N][MAX_N];//catlans[i][0] save the length

void calCatalans()
{
	short carry = 0, len = 1;
	catalan[1][0] = 1; catalan[1][1] = 1;
	catalan[2][0] = 1; catalan[2][1] = 2;
	for (int i = 3; i < MAX_N; i++)
	{
		carry = 0;
		for (int j = 1; j <= len; j++)//高精度乘以一个整数
		{
			short sum = catalan[i-1][j]*((i<<2)-2) + carry;
			carry = sum / 10;
			catalan[i][j] = sum % 10;
		}
		while (carry)
		{
			catalan[i][++len] = carry % 10;
			carry /= 10;
		}
		for (int j = len; j > 0; j--)//高精度除以一个整数
		{
			short sum = catalan[i][j] + carry*10;
			catalan[i][j] = sum / (i+1);
			carry = sum % (i+1);//一定能除尽。故此无需考虑余数情况
		}
		while (catalan[i][len] == 0) len--;
		catalan[i][0] = len;
	}
}

int main()
{
	calCatalans();
	int n;
	while (~scanf("%d", &n))
	{
		for (int i = catalan[n][0]; i > 0; i--)
		{
			printf("%d", catalan[n][i]);
		}
		putchar('\n');
	}
	return 0;
}
时间: 2024-12-13 23:43:05

HDU 1023 Train Problem II 大数打表Catalan数的相关文章

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

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 )

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

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]

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

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