题目链接: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
RPG专场练习赛
Recommend
lcy

题解:

1 卡特兰数的初步学习,卡特兰数应用 。

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)  ……此条计算公式容易溢出

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

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 35+10;
18
19 LL h[MAXN];
20
21 void init()
22 {
23     memset(h, 0, sizeof(h));
24     h[0] = 1; h[1] = 1;
25     for(int i = 2; i<MAXN; i++)
26         for(int j = 0; j<i; j++)
27             h[i] += 1LL*h[j]*h[i-j-1];
28 }
29
30 int main()
31 {
32     init();
33     int kase = 0, n;
34     while(scanf("%d", &n) && n!=-1)
35         printf("%d %d %lld\n", ++kase, n, 2LL*h[n]);
36 }

View Code

题目链接: 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
The 2011 Rocky Mountain Regional Contest
Recommend
lcy

题解:

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

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 30+10;
18
19 LL h[MAXN];
20
21 void init()
22 {
23     memset(h, 0, sizeof(h));
24     h[0] = 1;
25     for(int i = 1; i<MAXN; i++)
26         for(int j = 0; j<i; j++)
27             h[i] += 1LL*h[j]*h[i-j-1];
28 }
29
30 int main()
31 {
32     init();
33     int n;
34     while(scanf("%d", &n) && n)
35         printf("%lld\n", h[n]);
36 }

View Code

题目链接: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
Asia 2004, Shanghai (Mainland China), Preliminary
Recommend
Eddy

题意:

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

题解:

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

递推式一:

 1 import java.util.Scanner;
 2 import java.math.BigInteger;
 3
 4 public class Main {
 5
 6     public static void main(String[] args){
 7
 8         BigInteger[] a = new BigInteger[105];
 9
10         a[0] = BigInteger.ONE;
11         for(int i=1; i<=100; i++) {
12             a[i] = BigInteger.valueOf(0);
13             for(int j=0;j<i;j++){
14                 a[i] = a[i].add(a[j].multiply(a[i-j-1]));
15             }
16         }
17
18         Scanner input = new Scanner(System.in);
19         while(input.hasNext()){
20             int n=input.nextInt();
21             if(n==-1) break;
22             System.out.println(a[n]);
23         }
24     }
25 }

View Code

递推式二:

 1 import java.util.Scanner;
 2 import java.math.BigInteger;
 3
 4 public class Main {
 5
 6     public static void main(String[] args){
 7
 8         BigInteger[] a = new BigInteger[105];
 9
10         a[0] = BigInteger.ONE;
11         for(int i=1; i<=100; i++) {
12             a[i] = a[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
13         }
14
15         Scanner input = new Scanner(System.in);
16         while(input.hasNext()){
17             int n=input.nextInt();
18             if(n==-1) break;
19             System.out.println(a[n]);
20         }
21     }
22 }

View Code

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8324436.html

卡特兰数 HDU2067 HDU4165 HDU1134相关推荐

  1. HDU2067(卡特兰数)

    Problem Descrption 小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望.不过没过几天发现了棋盘的好玩之处.从起点(0,0)走到终点 ...

  2. 卡特兰数(n个节点的二叉树情况数量+hdu2067) (超级卡特兰数(施罗德数))

    卡特兰数是组合数学中一个常出现在各种计数问题中的数列. 举个栗子:1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742 ...

  3. Catalan Numbers 卡特兰数

    卡特兰数源于组合数学,递推式为 H[1] = 1:H[n] = H[n-1]*(4*n-2)/(n+1){n>=2}: 卡塔兰数的渐近增长为 下面给出几个求卡特兰数的公式,用h(n)表示卡特兰数 ...

  4. 错排、卡特兰数、斯特林数小结

    一. 错排 1.计算公式: 1) D[n] = (n-1)*(D[n-1]+D[n-2]) ,n>=2, D[0] = 1, D[1] = 0 . 解释:对于第n个要加入错排的数,它可以和已经错 ...

  5. 卡特兰数递推公式证明及应用

    目录 卡特兰数 定义 递推公式 公式1: 公式2: 公式3: 公式4: 应用场景 公式1证明 公式2证明 公式3证明 公式4证明 例题 卡特兰数 定义 在oeis上可以看到卡特兰数的定义如下. 递推公 ...

  6. Catalan数——卡特兰数

    今天阿里淘宝笔试中碰到两道组合数学题,感觉非常亲切,但是笔试中失踪推导不出来 后来查了下,原来是Catalan数.悲剧啊,现在整理一下 Catalan数--卡特兰数] 一.Catalan数的定义令h( ...

  7. 【COGS】2287:[HZOI 2015]疯狂的机器人 FFT+卡特兰数+排列组合

    [题意][COGS 2287][HZOI 2015]疯狂的机器人 [算法]FFT+卡特兰数+排列组合 [题解]先考虑一维的情况,支持+1和-1,前缀和不能为负数,就是卡特兰数的形式. 设C(n)表示第 ...

  8. 关于卡特兰数及典型例题

    关于卡特兰数: f[0] = 1, f[1] = 1; for(int i = 2; i <= n; i++)for(int j = 0; j < i; j++)f[i] += f[j] ...

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

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

最新文章

  1. 你有没有成为技术作家的潜力
  2. VTK:背景渐变BackgroundGradient用法实战
  3. Docker-Compose 一键部署Ningx+.Net Core+Redis集群
  4. 物联网基础知识_联网| 基础知识能力问答 套装1
  5. include.cpp(main函数的cpp文件)文件中包含另一个.cpp文件的错误及原因
  6. php 修改密码提示,修改密码通知
  7. vue js中解决二进制转图片显示问题
  8. 二级域名共享cookies
  9. SPSS PROCESS插件安装及使用【SPSS 046期】
  10. 神经网络绘图软件推荐合集
  11. QT高级编程技巧(一)-- 编写高效的signal slot通信代码
  12. eclipse配置java环境变量_eclipse配置环境变量
  13. 金蝶k3远程组件配置连接服务器,金蝶K3服务器配置工具
  14. 电脑重装系统步骤图解,简单安全一目了然
  15. java检测kafka是否连接成功,Kafka 消费者失败检测
  16. 理财入门:企业分析(简述)
  17. VS2013打包Windows程序部署教程
  18. 系统性思考-思考习惯的养成
  19. 苹果电脑如何使用Siri语音助手!
  20. 客户说我已经有合作伙伴了 电话销售如何回应

热门文章

  1. 本地方法栈线程公有_Java运行时区域,哪些区域是线程私有的?哪些是共有的?...
  2. c语言中栈堆,C语言中堆和栈的区别
  3. python含多个附件的邮件_Python发送带有多个图像附件的电子邮件
  4. java语言基本语法_Java语言基本语法
  5. oracle 指定格式化,Oracle中的格式化函数
  6. vb.net mysql存储图片_怎么让VB.NET 上传图片到SQL 数据库只保存路径,图片保存到文件...
  7. NYOJ 27 大数阶乘
  8. LeetCode 110. 平衡二叉树思考分析
  9. 在网络中配置思科交换机
  10. Bean的scope属性