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 ...
随机推荐
- [C++程序设计]函数模板
定义函数模板的一般形 式为 template < typename T> 或 template <class T> 函数模板: 函数参数个数,函数体相同.参数类型不同 函数重载 ...
- Linux GCC
//test.c #include <stdio.h> int main() { printf("Hello World!\n"); ; } One Shot gcc ...
- python bottle框架(WEB开发、运维开发)教程
教程目录 一:python基础(略,基础还是自己看书学吧) 二:bottle基础 python bottle web框架简介 python bottle 框架环境安装 python bottle 框架 ...
- Linux实现SSH无密码登录(对目录权限的设置非常详细,可以参考一下)
假设服务器IP地址为192.168.1.1,机器名:cluster.hpc.org 客户端IP地址为172.16.16.1,机器名:p470-2.wangrx.sioc.ac.cn 客户端用户yzha ...
- android 控件的移动
android 控件的移动 1. Java代码 imagebutton.setOnTouchListener(newOnTouchListener(){ int[]postion=newint[]{0 ...
- UVA 11111-Generalized Matrioshkas(栈)
题意:有很多层盒子,盒子里面再套盒子,一个盒子可能套多个独立的子盒子,但子盒子的总体积必须小于该盒子,否则不合法,输入给一行数,负数代表左边,正数代表右边,大小表示其体积,如-2,-1,1,2则表示体 ...
- Zookeeper 5、Zookeeper应用场景
应用场景1 .统一命名服务 » 分布式应用中,通常需要有一套完整的命名规则,既能够产生唯一的名称又便于人识别和记住,通常情况 下用树形的名称结构是一个理想的选择,树形的名称结构是一个有层次的目录结构, ...
- [Spring boot] web应用返回jsp页面
同事创建了一个spring boot项目,上传到svn.需要我来写个页面.下载下来后,始终无法实现在Controller方法中配置直接返回jsp页面. 郁闷了一下午,终于搞定了问题.在此记录一下. 目 ...
- PHP 超强过滤函数
PHP 超强过滤函数 你有每次要过滤的时候总是去翻曾经的过滤代码的时候么? 你有搜索过怎样防过滤,防攻击的PHP解决方法么? 你有对全然遵循'过滤输入,避免输出',Web界经典说辞么? 事实上 ...
- 提交App,请求Apple加急审核
转载自:http://blog.csdn.net/showhilllee/article/details/19541493 提交完毕后进入加急审核页面. 链接:https://developer.ap ...