(母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023
Train Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10372 Accepted Submission(s): 5543
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.
注意:
这是卡特兰数,用大数。使用的公式为:
h(n)=h(n-1)*(4*n-2)/(n+1)。
用JAVA更简单:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
BigInteger a[]=new BigInteger[101];
a[0]=BigInteger.ZERO;
a[1]=BigInteger.ONE;
for(int i=2;i<=100;i++)
a[i]=a[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
while(in.hasNextInt()) {
int n=in.nextInt();
System.out.println(a[n]);
}
}
}
接下来是C++:
#include <iostream>
#include <cstdio>
using namespace std;
int a[][];
int i,j,n;
void ctl() //打表。
{
a[][]=;
a[][]=;
a[][]=;
a[][]=;
int len=,t,yu=; //注意初始化。
for(i=;i<;i++) //先打表求出1到100的所有Catalan数。
{
for(j=;j<=len;j++) //大数乘法。
{
t=a[i-][j]*(*i-)+yu;
yu=t/;
a[i][j]=t%; //求每位数的确定的数。
}
while(yu) //进位。一直到yu为0为止。
{
a[i][++len]=yu%;
yu/=;
}
for(j=len;j>=;j--) //大数除法。联系手工除法步骤。
{
t=a[i][j]+yu*;
a[i][j]=t/(i+); //可以看做手工除法的商。
yu=t%(i+); //可以看做手工除法的余数。
}
while(!a[i][len]) //去掉前边的0。
{
len--;
}
a[i][]=len;
}
}
int main()
{
ctl();
int n;
while(~scanf("%d",&n))
{
for(int i=a[n][];i>;i--)
{
printf("%d",a[n][i]);
}
puts("");
}
return ;
}
(母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023的更多相关文章
- HDU 1023 Train Problem II (大数卡特兰数)
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Train Problem II(卡特兰数+大数乘除)
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- C - Train Problem II——卡特兰数
题目链接_HDU-1023 题目 As we all know the Train Problem I, the boss of the Ignatius Train Station want to ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- Train Problem II(卡特兰数 组合数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- HDU——1023 Train Problem II
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1023 Train Problem II 大数打表Catalan数
一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n ...
- 求解Catalan数,(大数相乘,大数相除,大数相加)
Catalan数 卡塔兰数是组合数学中一个常在各种计数问题中出现的数列.以比利时的数学家欧仁·查理·卡塔兰(1814–1894)命名.历史上,清代数学家明安图(1692年-1763年)在其<割圜 ...
随机推荐
- THE First Individual Project - Word frequency program
第一次写博客,这次也是本学期写到第一个程序. 老师要求网址:http://www.cnblogs.com/jiel/p/3311400.html#2777556 一.项目预计时间 一开始想使用不熟悉的 ...
- Linux内核分析作业第七周
一. 预处理.编译.链接 gcc hello.c -o hello. gcc编译源代码生成最终可执行的二进制程序,GCC后台隐含执行了四个阶段步骤. 预处理 → 编译 → 汇编 → 链接 预处理:编译 ...
- github链接与心得体会
https://github.com/wangyuefang/test 第一次使用github,我觉得github是一个非常人性化的软件,使用起来很方便.而且可以把GitHub作为免费的远程仓库,如果 ...
- LeetCode 363:Max Sum of Rectangle No Larger Than K
题目链接 链接:https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/description/ 题解&代码 1 ...
- IO学习
---恢复内容开始--- 命名规则: 1.类名使用 UpperCamelCase 风格,必须遵从驼峰形式,但以下情形例外:(领域模型 的相关命名)DO / BO / DTO / VO 等. 2.方法名 ...
- ADC转换的分辨率
分辨率是指ADC能够分辨量化的最小信号的能力.分辨率用二进制位数表示.例如对一个10位的ADC,其所能分辨的最小量化电平为参考电平(满量程)的2的10次方分之一.也就是说分辨率越高,就能把满量程里的电 ...
- shell脚本--文件测试
文件测试是指测试某一个文件或者目录是否存在 测试文件格式[ 操作符 目录或者文件 ] 注意左括号和操作符之间有一个空格,文件或者目录 与右边的括号之间也有一个空格. -d 测试是否为目录 -e ...
- Java Heap Dump On OutOfMemoryError
-XX:+HeapDumpOnOutOfMemoryError Batch "C:\Program Files\Java\jdk1.8.0_162\bin\java.exe" -X ...
- Netty4ClientHttpRequest代码赏析
private static int getPort(URI uri) { int port = uri.getPort(); if (port == -1) { if ("http&quo ...
- K3CLOUD替代方案
路径 [生产制造]->[工程数据]->[替代方案]->[替代方案] 应用场景 实际业务处理中,由于订单取消.工程变更.客户需求变化.预测或计划不准确等原因造成原材料库存积压.呆滞,使 ...