HDU 1023 Train Problem II

来源:互联网 发布:淘宝电子面单打印机 编辑:程序博客网 时间:2024/05/16 08:04
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
12310
 

Sample Output
12516796
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
原创粉丝点击