hdu 1023 Train Problem II catalan数列 大数乘除

Train Problem II

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2714Accepted Submission(s): 1517


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

很久写博客了。 今天下午都没课。刷了下hdu。

这道题是一道catalan数列的题。之前做的是hdu 1022train problem I 。 那题简单的模拟入栈和出栈。

刚做这道题时,我用递归的思维,只推出f[n]=f[1]*f[n-1]+f[2]*f[n-2]+···f[n-1]*f[1] 然后 用循环打表。但没考虑大数。WA了。 从网上查了catalan 数的资料。才查出catalan数的递归的另一个表达式

f[n]=f[n-1]*(4*n-2)/(n+1),貌似这个表达式是根据关系式 f[n]=f[1]*f[n-1]+f[2]*f[n-2]+···f[n-1]*f[1], 用数学方法推出的,我没看懂。 网上可以查到。同时有个收获 :ACM中 catalan 数列相关的问题很多,日后继续学习。

catalan数相关链接 http://baike.baidu.com/view/1396146.htm

http://www.cppblog.com/MiYu/archive/2010/08/07/122573.html

还有就是用到了大数的乘除 。

下面是代码和思路

#include<stdio.h> // Status_AC Time_0ms Memory_236k int a[105][105]={{0}}; //用来保存catalan数列 的数组。 int main() { int n,i,j,temp=0,digit=1,k,m,s; int len[100]={0}; //用来保存各个数的位数。 a[1][0]=1; //第一个catalan数赋为1 len[1]=1; //第一个数的位数 for(i=2;i<=100;i++) { k=4*i-2; //k,m是关系式中的参数。 m=i+1; //下面这段是大数乘。具体思路 关于求N!的博文中有写 for(j=0;j<digit;j++) { s=a[i-1][j]*k+temp; //s只是个临时变量 a[i][j]=s%10; temp=s/10; } while(temp) { a[i][digit++]=temp%10; temp/=10; } // 大数除法。 for(j=digit-1;j>=0;j--) { s=a[i][j]%m; a[i][j]/=m; if(s) a[i][j-1]+=(s*10); } //除掉高位的0 while(!a[i][digit-1]) digit--; len[i]=digit; //记录数位 } while(scanf("%d",&n)!=EOF) { for(i=len[n]-1;i>=0;i--) printf("%d",a[n][i]); printf("\n"); } return 0; }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值