Train Problem II
问题陈述:
问题解析:
卡特兰数(Catalan)的应用
基本性质:
f(n) = f(1)f(n-1) + f(2)f(n-2) + ... + f(n-2)f(2) + f(n-1)f(1);
f(n) = C(2n, n) / (n+1) = C(2n-2, n-1) / n;
Cn = (4n-2)/(n+1) Cn-1
代码详解:
I: C++
#include <iostream>
#include <cstdio>
#include <memory.h> using namespace std; #define MAX 101
#define BASE 10000 void multiply(int a[], int len, int b) {
for(int i=len-, carry=; i>=; i--) {
carry += b * a[i];
a[i] = carry % BASE;
carry /= BASE;
}
} void divide(int a[], int len, int b) {
for(int i=, div=; i<len; i++) {
div = div * BASE + a[i];
a[i] = div / b;
div %= b;
}
} int main()
{
int i, j, h[][MAX];
memset(h[], , MAX*sizeof(int));
for(i=, h[][MAX-]=; i<=; i++) {
memcpy(h[i], h[i-], MAX*sizeof(int));
multiply(h[i], MAX, *i-);
divide(h[i], MAX, i+);
} while(cin >> i && i>= && i <= ) {
for(j=; j<MAX && h[i][j]==; j++);
printf("%d", h[i][j++]);
for(; j<MAX; j++)
printf("%04d", h[i][j]);
cout << endl;
}
return ;
}
II: Java
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int n;
while(sc.hasNext()) {
BigInteger catalan = BigInteger.valueOf(1);
n = sc.nextInt();
for(int i=1; i<=n; i++) {
catalan = catalan.multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
}
System.out.println(catalan);
}
sc.close();
}
}
参考资料:
转载请注明出处:http://www.cnblogs.com/michaelwong/p/4346692.html
Train Problem II的更多相关文章
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- Train Problem II(卡特兰数+大数乘除)
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- (母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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(卡特兰数 组合数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...
- HDU——1023 Train Problem II
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 1023 Train Problem II(卡特兰数)
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...
- ACM学习历程—HDU1023 Train Problem II(递推 && 大数)
Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know ...
- Train Problem II HDU 1023 卡特兰数
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...
随机推荐
- Oracle数据库之间数据同步
这段时间负责某个项目开发的数据库管理工作,这个项目中开发库与测试数据库分离,其中某些系统表数据与基础资料数据经常需要进行同步,为方便完成指定数据表的同步操作,可以采用dblink与merge结合的方法 ...
- 无法添加sql server ER图
Database diagram support objects cannot be installed because this database does not have a valid own ...
- Ext4报错Uncaught Ext.Loader is not enabled
提示: Uncaught Ext.Loader is not enabled, so dependencies cannot be resolved dynamically. Missing requ ...
- T-SQL 创建、修改、删除数据库,表语法
CREATE 语句 CREATE语句的开头都是一样的,然后是特定的细节. CREATE <object type> <object name> 一.CREATE DATABAS ...
- Linux下ld搜索问题:ld: cannot find -l"XX"
ld命令行工具(链接库的一个工具)的搜索路径是-L指定的,库名是-l指定的. 比如: ld -L[dir] -l[mylib] --verbose 以上我用可视化的方法显示ld的搜索路径,其结果是居然 ...
- eclipse的优化 gc.log
原帖:http://www.javaeye.com/topic/756538 性能优化从身边做起. 首先建立评估体系,将workspace里所有的项目close掉,关闭eclipse.优化的用例就是启 ...
- windows phone中,将crash report记录下来,写入文件,方便分析
APP出现crash(崩溃)总是不能忍的 当我们连接调试器调试的时候,发现每当APP崩溃的时候 程序都会走到App.xaml.cs中的 // Code to execute on Unhandled ...
- JS代码混淆 支持PHP .NET PERL
官方 http://dean.edwards.name/packer/ Also available as .NET, perl and PHP applications. .NET实例下载地址:h ...
- ZOJ3578(Matrix)
Matrix Time Limit: 10 Seconds Memory Limit: 131072 KB A N*M coordinate plane ((0, 0)~(n, m)). I ...
- UVA 10282 (13.08.18)
Problem C: Babelfish You have just moved from Waterloo to a big city. The people here speakan incomp ...