HDU 1023 Train Problem II

38 篇文章 0 订阅
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  1130  1131  2067
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
比上一道题还模板的模板题……好像卡特兰数就是与高精度连在一起的?
主要是思路,好神奇的样子……

求出栈序列,比如1,2,3,出栈序列为3 2 1,1 2 3,1 3 2,2 1 3,2 3 1,一共5种
我们把入栈看做1,出栈看做0,那么入栈出栈看做一系列的1010……,但是必须保证从左往右
看的时候1必须多余0,这个是卡特兰数的第二个应用,种数为:C(n,2n)-C(n+1,2n).
粗略这样理解:我们从2n个位置中选出n个来存放1,方法数为C(n,2n),减去不满足的情况。
不合法的情况:我们在2n个位置放n+1个0,n-1个1,由于0的个数多2个,2n为偶数,故必在某一个奇数位上出现0的累计数超过1的累计数。同样在后面部分0和1互换,使之成为由n个0和n个1组成的2n位数,即n+1个0和n-1个1组成的2n位数必对应一个不符合要求的数,即C(n+1,2n)。
h(n)=C(n,2n)-C(n+1,2n).

#include<cstdio>

int n,a[101][101];

void findd()
{
	a[1][1]=1;
	for(int i=2;i<=100;i++)  //乘4i-2除以i+1  
	{
		int x=0,z=4*i-2;
		for(int j=1;j<=100;j++)
		{
			a[i][j]=a[i-1][j]*z+x;
			x=a[i][j]/10;a[i][j]%=10;
		}
		x=0,z=i+1;
		for(int j=100;j>=1;j--)
		{
			int k=a[i][j];
			a[i][j]=(x*10+k)/z;
			x=(x*10+k)%z;
		}
	}
}

int main()
{
	findd();
	while(scanf("%d",&n)==1)
	{
		int i=n;
		while(a[n][i]==0) i--;
		for(;i>=1;i--) printf("%d",a[n][i]);printf("\n");
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值