题目链接:https://vjudge.net/problem/HDU-2067

小兔的棋盘

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11800    Accepted Submission(s): 5952

Problem Description
小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望。不过没过几天发现了棋盘的好玩之处。从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n),现在小兔又想如果不穿越对角线(但可接触对角线上的格点),这样的路径数有多少?小兔想了很长时间都没想出来,现在想请你帮助小兔解决这个问题,对于你来说应该不难吧!
 
Input
每次输入一个数n(1<=n<=35),当n等于-1时结束输入。
 
Output
对于每个输入数据输出路径数,具体格式看Sample。
 
Sample Input
1
3
12
-1
 
Sample Output
1 1 2
2 3 10
3 12 416024
 
Author
Rabbit
 
Source
 
Recommend
lcy

题解:

卡特兰数的初步学习卡特兰数应用

2.卡特兰数计算公式:

1) h(n) = h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (n>=1) , 其中 h[0] = 1;

2) h(n) = c(2n,n) - c(2n,n+1)(n=0,1,2,...) <==> h(n) = C(2n,n)/(n+1)

3) h(n) = h(n-1)*(4*n-2) / (i+1)  ……此条计算公式容易溢出

注意:卡特兰数的计算很容易溢出。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; LL h[MAXN]; void init()
{
memset(h, , sizeof(h));
h[] = ; h[] = ;
for(int i = ; i<MAXN; i++)
for(int j = ; j<i; j++)
h[i] += 1LL*h[j]*h[i-j-];
} int main()
{
init();
int kase = , n;
while(scanf("%d", &n) && n!=-)
printf("%d %d %lld\n", ++kase, n, 2LL*h[n]);
}

题目链接: https://vjudge.net/problem/HDU-4165

Pills

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1626    Accepted Submission(s): 1139

Problem Description
Aunt Lizzie takes half a pill of a certain medicine every day. She starts with a bottle that contains N pills.

On the first day, she removes a random pill, breaks it in two halves, takes one half and puts the other half back into the bottle.

On subsequent days, she removes a random piece (which can be either a whole pill or half a pill) from the bottle. If it is half a pill, she takes it. If it is a whole pill, she takes one half and puts the other half back into the bottle.

In how many ways can she empty the bottle? We represent the sequence of pills removed from the bottle in the course of 2N days as a string, where the i-th character is W if a whole pill was chosen on the i-th day, and H if a half pill was chosen (0 <= i < 2N). How many different valid strings are there that empty the bottle?

 
Input
The input will contain data for at most 1000 problem instances. For each problem instance there will be one line of input: a positive integer N <= 30, the number of pills initially in the bottle. End of input will be indicated by 0.
 
Output
For each problem instance, the output will be a single number, displayed at the beginning of a new line. It will be the number of different ways the bottle can be emptied.
 
Sample Input
6 1 4 2 3 30 0
 
Sample Output
132 1 14 2 5 3814986502092304
 
Source
Recommend
lcy

题解:

有n片药,每天吃半片。当天要么在药罐中抽到把一片完整的药片,然后分成两半,吃一半,最后把另一半放回药罐中;要么抽到半片药片直接吃。问:有多少种情况? 单纯的卡特兰数。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; LL h[MAXN]; void init()
{
memset(h, , sizeof(h));
h[] = ;
for(int i = ; i<MAXN; i++)
for(int j = ; j<i; j++)
h[i] += 1LL*h[j]*h[i-j-];
} int main()
{
init();
int n;
while(scanf("%d", &n) && n)
printf("%lld\n", h[n]);
}

题目链接:https://vjudge.net/problem/HDU-1134

Game of Connections

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

Problem Description
This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.

It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

 
Input
Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.
 
Output
For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
 
Sample Input
2 3 -1
Sample Output
2 5
Source
 
Recommend
Eddy

题意:

1~2*n 顺时针排列成一圈, 用n条线段连接n对数,要求线段不能有交叉,问:有多少种连接情况?

题解:

可以将此题联想到出栈问题,这样就转化成卡特兰数了。

递推式一:

 import java.util.Scanner;
import java.math.BigInteger; public class Main { public static void main(String[] args){ BigInteger[] a = new BigInteger[]; a[] = BigInteger.ONE;
for(int i=; i<=; i++) {
a[i] = BigInteger.valueOf();
for(int j=;j<i;j++){
a[i] = a[i].add(a[j].multiply(a[i-j-]));
}
} Scanner input = new Scanner(System.in);
while(input.hasNext()){
int n=input.nextInt();
if(n==-) break;
System.out.println(a[n]);
}
}
}

递推式二:

 import java.util.Scanner;
import java.math.BigInteger; public class Main { public static void main(String[] args){ BigInteger[] a = new BigInteger[]; a[] = BigInteger.ONE;
for(int i=; i<=; i++) {
a[i] = a[i-].multiply(BigInteger.valueOf(*i-)).divide(BigInteger.valueOf(i+));
} Scanner input = new Scanner(System.in);
while(input.hasNext()){
int n=input.nextInt();
if(n==-) break;
System.out.println(a[n]);
}
}
}

卡特兰数 HDU2067 & HDU4165 & HDU1134的更多相关文章

  1. hdu2067 小兔的棋盘 DP/数学/卡特兰数

    棋盘的一角走到另一角并且不越过对角线,卡特兰数,数据量小,可以当做dp求路径数 #include<stdio.h> ][]; int main() { ; ) { int i,j; lon ...

  2. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  3. 卡特兰数(Catalan)

    卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名,其前几项为 : 1, 2, ...

  4. NOIP2003pj栈[卡特兰数]

    题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈). 栈的重要性不言自明,任何 ...

  5. 卡特兰数 (Catalan)

    卡特兰数:(是一个在计数问题中出现的数列) 一般项公式: 1.         或       2.   递归公式: 1.  或 2. 注:全部可推导. (性质:Cn为奇数时,必然出现在奇数项 2k- ...

  6. HDU 5673 Robot ——(卡特兰数)

    先推荐一个关于卡特兰数的博客:http://blog.csdn.net/hackbuteer1/article/details/7450250. 卡特兰数一个应用就是,卡特兰数的第n项表示,现在进栈和 ...

  7. HDU 1023 Traning Problem (2) 高精度卡特兰数

    Train Problem II Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Sub ...

  8. HDU1130 卡特兰数

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. LightOJ1170 - Counting Perfect BST(卡特兰数)

    题目大概就是求一个n个不同的数能构造出几种形态的二叉排序树. 和另一道经典题目n个结点二叉树不同形态的数量一个递推解法,其实这两个问题的解都是是卡特兰数. dp[n]表示用n个数的方案数 转移就枚举第 ...

随机推荐

  1. 洛谷—— P2117 小Z的矩阵

    https://www.luogu.org/problemnew/show/2117 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1 ...

  2. linux source filename

    简单来说:source filename即把filename文件里的命令(命令集脚本文件)执行一遍,相当于在shell里逐个执行单条命令

  3. IntelliJ IDEA重构技巧收集

    https://segmentfault.com/a/1190000002488608(重命名文件) http://www.jianshu.com/p/ab298b46bf50(快速生成方法) htt ...

  4. mac 安装opencv-python

    Mac下安装opencv-python 项目中使用的是opencv 2的版本,因此下面说一下opencv2的一些安装流程. 安装方法 1:如果没有homebrew的话,需要先安装 安装命令: ruby ...

  5. 在CentOS上安装 MongoDB

    安装是在线安装方式,因此必须先保证能正常上网. 安装mongodb,官方的安装文档,是在线安装方式: https://docs.mongodb.com/manual/tutorial/install- ...

  6. How to Create a Provisioning Profile for iPhone

    If you're making iPhone and iPad apps, there are some processes you must work through to go from dev ...

  7. 椭圆人头跟踪bmp图像序列 BMP Image Sequences for Elliptical Head Tracking

    BMP Image Sequences for Elliptical Head Tracking The BMP image sequences used in the head tracking d ...

  8. Android Studio 设置项目Module编码,解决Android Studio项目执行时乱码问题

    Android Studio的项目设置逻辑与Eclipse有非常大的差别.运行的操作为File->Setting->File Encodings然后来进行设置,如图所看到的: waterm ...

  9. Spring学习之事务注解@Transactional

    今天学习spring中的事务注解,在学习Spring注解事务之前需要明白一些事务的基本概念: 事务:并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通 ...

  10. man screen

    http://www.gnu.org/software/screen/manual/screen.html Screen User's Manual Next: Overview, Previous: ...