HDU1023-Train Problem II(大数+卡特兰数+递推)

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.

分析:

题意:
一列长度单调递增的火车按顺序进入火车站,问出火车站可能的顺序有多少种?

上网查了一下,是卡塔兰数,卡塔兰数是什么?我也讲不清楚,各自百度好吧!记住公式就好:h(i)=h(i-1)(4i-2)/(i+1);注意了,这里数据很大,要用到高精度算法,但是(4i-2)/(i+1)可能算给出来是小数,所以我们要把这个公式拆开来算!先算(4i-2)*h(i-1),然后再做除的操作!

代码:

#include<iostream>
#include<cstdio>
#define N 105

using namespace std;

int dp[N][N];

int main()
{
	int n;
	dp[1][0]=dp[1][1]=1;
	for(int i=2;i<=100;i++)
	{
		int len=dp[i-1][0],v=0;
		int k=(4*i-2);
		for(int j=1;j<=len;j++)
		{
			dp[i][j]=k*dp[i-1][j]+v;
			v=dp[i][j]/10;
			dp[i][j]%=10;
		}
		while(v)
		{
			dp[i][++len]=v%10;
			v/=10;
		}
		for(int j=len;j>0;j--)
		{
			v=v*10+dp[i][j];
			dp[i][j]=v/(i+1);
			v%=(i+1);
		}
		while(!dp[i][len])
		{
			len--;
		}
		dp[i][0]=len;
	}
	while(~scanf("%d",&n))
	{
		for(int i=dp[n][0];i>0;i--)
		{
			printf("%d",dp[n][i]);
		}
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值