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 ...
随机推荐
- PIL参考手册
Python Imaging Library Handbook http://effbot.org/imagingbook/pil-index.htm 随机验证码的产生 import Image, I ...
- sql helper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- Effective Java从零开始 - 就是爱Java
或许你已经开始写Java了,或许只是想要一窥这个的世界,无论是抱着何种心情来看Java,从零开始,会一种没有负担,没有包袱的事,你会发现写程序不再是枯燥乏味,孤单寂寞的一个人,而是生活中最快乐的学习之 ...
- 子shell的$$
http://blog.csdn.net/firefoxbug/article/details/7426109
- Qt Project的持续集成方案
作者:齐亮链接:http://www.zhihu.com/question/24314354/answer/27547787来源:知乎著作权归作者所有,转载请联系作者获得授权. PETER HARTM ...
- jQuery中的选择器《思维导图》
学习jQuery的课程中,我对jQuery中的选择器有了更深的认识,它的简洁写法,完美的兼容性,可靠的处理机制,都让我们省了很多事, 下面是我在学习过程中对jQuery选择器写的思维导图(全屏查看:& ...
- Linux下配置C/C++开发环境-----Eclipse
先自己去官网下载jdk. 解压即安装. 然后在~/.bashrc文件里面配置下环境变量就行了.在文件最后添加以下路径,如下: JAVA_HOME=/home/username/jdk/jdk1.8.0 ...
- g++ error: expected ‘)’ before ‘*’ token
原本*号前面的类型是我用typedef自定义的类型的,MyType* const p: 发生这样的错误是,编译器根本不知道MyType是什么东西,这是我在C++多重继承中遇到的.MyType是我在基类 ...
- iOS开发之property属性介绍
大家都知道@property和@synthesize可以自动生成某个类成员变量的存取方法,但可能对property中的一些属性不是很了解,网上的一些介绍有的不是很正确,感觉会误导新手,于是准备详细介绍 ...
- css案例学习之div+a实现菜单
效果 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...