《组合数学》——卡特兰数
我们结合一个题目具体看看Catalan数的应用。(Pr0blem source:hdu2067)
Q3:1~n按照次序进栈,那么有多少种不同的出栈方案?
不妨来看一道关于出入栈方案数的题目(Problem source:hdu1023)
题目大意:就是n辆货车,他们进栈的顺序有多少种。 数理分析:这里轨道只有一条,这条轨道就模拟了Q3模型中的栈,而这里想求的其实就是一个顺序入栈而后不同次序出栈的方案数。 编程实现:组合数学中给出的计数公式随着n的扩大,数值一般会爆掉c/c++中的数据类型,于是这里为了解决大数问题,我们用JAVA的写法来实现。
import java.io.*; import java.util.*; import java.math.BigInteger;public class Main {public static void main(String args[]){ BigInteger[] a = new BigInteger[101];a[0] = BigInteger.ZERO;a[1] = BigInteger.valueOf(1);for(int i = 2; i <= 100; ++i)a[i] = a[i - 1].multiply(BigInteger.valueOf(4 * i - 2)).divide(BigInteger.valueOf(i+1));Scanner in = new Scanner(System.in);int n;while(in.hasNext()){n = in.nextInt();System.out.println(a[n]);}} }
Q4:给定一个数n,在一个圆上我们找到2n个点,然后成对的连接两个点,我们可以得到多少种方案数?
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?
可以看到,这里的问题是和Q4一模一样的问题,这里只需编程实现Catalan数即可。
Q5:给点节点数n,我们用这n个节点能够生成多少个二叉树? 针对这个问题,我们还是基于上文一开始给出Catalan数的思维角度(Q1分割n边形的问题),同样,为了记数的方便和最终结论的他统一,这里我们来分析n+1个节点能够生成多少个二叉树。 既然是生成二叉树,其必含有根节点、左子树、右子树。这里除去根节点,左子树和右子树一共还有n个节点,我们假设左子树有k个节点,那么右子树就有n-k个节点,这里我们再假设h(n)表示n各节点对应的方案数,那么这种情况下二叉树不同的种类就有h(k) * h(n - k),那么再依次遍历k的值进行累加,我们就得到了递推式——h(n+1) = ∑h(k)h(n-k),这里k的范围是[0,n]。 这其实又回到了Q1所进行的推导。 最终我们可以得出结论,凸n+1边形分隔成三角形且对角线不相交的方案数,和n+1个节点形成二叉树的种类数是一样的,都对应第n个Catalan数。
让我们找一道相关的题目实践一下我们的结论。(Problem source:hdu1130)
Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?
Q6:小明住在市中心北n个街区,东n个街区,那么小明要穿越街区到市中心上班,有多少条路可以走?
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?
我们再来看一道关于Catalan数变形应用的问题。(Problem source : hdu 3723)
The researchers have discovered a new kind of species called "otaku", whose brain waves are rather strange. The delta wave of an otaku's brain can be approximated by a polygonal line in the 2D coordinate system. The line is a route from point (0, 0) to (N, 0), and it is allowed to move only to the right (up, down or straight) at every step. And during the whole moving, it is not allowed to dip below the y = 0 axis.
For example, there are the 9 kinds of delta waves for N = 4:Given N, you are requested to find out how many kinds of different delta waves of otaku.
通过推导我们可以看到,我们得到了新的齐次递推式
import java.math.BigInteger; import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);BigInteger mod = BigInteger.TEN.pow(100);BigInteger sum = new BigInteger("0");BigInteger t = new BigInteger("0");while (sc.hasNext()) {int n = sc.nextInt();sum = BigInteger.ONE;t = BigInteger.ONE;for(int k = 1; k + k <= n; k++) {t = t.multiply(BigInteger.valueOf((n-2*k+1)*(n-2*k+2))).divide(BigInteger.valueOf(k*(k+1)));sum = sum.add(t);}System.out.println(sum.mod(mod));}} }
我们继续来看一道有关Catalan数的问题(Prblem sourece:hdu 1133)
Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).
Now the problem for you is to calculate the number of different ways of the queue that the buying process won't be stopped from the first person till the last person. Note: initially the ticket-office has no money.
The buying process will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.
题目大意:就是有m个人拿50元,n个人拿100元,他们通过自动取票机买一张50元的票,但是你这个取票机是空的,问你有多少种方案,是的这m+n个人可以都得到票。 数理分析:如果m<n,是没有符合要求的方案数的,这显而易见。如果这里是2n个人,n个50块n个100块,那么这就是很典型诶对Catalan数的模型了,它的分析思路很类似关于出入栈顺序的模型。但是这里,还是有一定的区别的,但是我们可以用相同的思想进行分析。
import java.util.*; import java.math.BigInteger; public class Main{public static void main(String[] args){int a,b;Scanner in=new Scanner(System.in);int cnt=0;while(in.hasNext()){cnt++;a=in.nextInt();b=in.nextInt();BigInteger ans=BigInteger.ONE;if(a==0&&b==0)break;if(a<b) ans=BigInteger.ZERO;else {for(int i=1;i<=a+b;i++){ans=ans.multiply(BigInteger.valueOf(i));}int mpl=(a-b+1);int dvd=(a+1);ans=ans.multiply(BigInteger.valueOf(mpl));ans=ans.divide(BigInteger.valueOf(dvd));}System.out.println("Test #"+cnt+":");System.out.println(ans);}} }
参考系:《组合数学》Richard
转载于:https://www.cnblogs.com/rhythmic/p/5472575.html
《组合数学》——卡特兰数相关推荐
- 算法基础 - 数论 | 组合数学 卡特兰数(Catalan number)定义、证明及例题
写在前面:卡特兰数这东西感觉挺常用的,并且公式很简单,那就花一下午总结一下,学点皮毛吧(反正遇到我还是不会 ) [PDF] 大三上组合数学课堂讲义 文章目录 卡特兰数定义 卡特兰数的性质 卡特兰数证明 ...
- 知识点 组合数学 卡特兰数
关于卡特兰数 卡特兰数是一种经典的组合数,经常出现在各种计算中,其前几项为 : 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, ...
- 【组合数学】卡特兰数 / 大施罗德数 相关
在组合数学中,施罗德数用来描述从 (0,0)(0, 0)(0,0) 到 (n,n)(n,n)(n,n) 的网格中,只能使用 (1,0)(1,0)(1,0).(0,1)(0,1)(0,1).(1,1)( ...
- Nowcoder 5477E. 弦(卡特兰数、组合数学)
题目描述: 给定一个圆,圆上有2N个互不重叠的点.每次操作随机选择两个先前未选择过的点连一条弦,共连成N条弦,求所有弦不交的概率. 输入描述: 一行,只有一个整数N(1≤N≤10^7). 输出描述: ...
- Train Problem II(卡特兰数 组合数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...
- Catalan Numbers 卡特兰数
卡特兰数源于组合数学,递推式为 H[1] = 1:H[n] = H[n-1]*(4*n-2)/(n+1){n>=2}: 卡塔兰数的渐近增长为 下面给出几个求卡特兰数的公式,用h(n)表示卡特兰数 ...
- 错排、卡特兰数、斯特林数小结
一. 错排 1.计算公式: 1) D[n] = (n-1)*(D[n-1]+D[n-2]) ,n>=2, D[0] = 1, D[1] = 0 . 解释:对于第n个要加入错排的数,它可以和已经错 ...
- HDU 5673 Robot 卡特兰数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题目描述: 一个人从原点开始向右走, 要求N秒后回到原点, 且过程中不能到负半轴, 人有两种操 ...
- 卡特兰数(简单说说)
参考题解: [算法]震惊!!!史上最详细的卡特兰数浅谈!!! 卡特兰数(好像很有用的说) 介绍 卡特兰数是组合数学中一种著名数列,其前几项为: 1, 2, 5, 14, 42, 132, 429, 1 ...
- 信奥中的数学:斯特林数、卡特兰数
P1287 盒子与球(球不同 盒不同 不允许有空盒) 盒子与球 - 洛谷 第二类斯特林数总结 第二类斯特林数总结 - _zjz 的博客 - 洛谷博客 P4091 [HEOI2016/TJOI2016] ...
最新文章
- 在Windows系统利用IP地址登陆Linux服务器
- 【排错】IDEA 提示 ‘Optional.get()‘ without ‘isPresent()‘ check错误
- Winform自动更新组件分享
- Nagios+pnp4nagios+rrdtool 安装配置nagios(一)
- ASPxGridView1单击,双击事件
- redhat7.1安装mysql_redhat7.1 安装mysql 5.7.10步骤详解(图文详解)
- 一元二次方程python脚本_Python实现求解一元二次方程的方法示例
- 精通开关电源设计第二版pdf_11、秋招年35月准备期——Verilog HDL高级数字设计(第二版)...
- python django 快速实现注册,登录,注销
- java入门第六天课程_Java基础学习第六天 小游戏
- 解决jquery下checked取值问题...
- oracle-3-子查询和常用函数
- Linux学习之安装jdk
- mariadb.zip压缩包解压后使用绿色db配置方法
- AI中的图像识别技术的原理及过程
- 五、ELK设置用户密码登陆
- JavaScript基础系列之一 快速入门
- 金彩教育:提升产品标签的方法
- NEO主要技术社区成员大曝光
- 【异常处理】The CXX compiler identification is unknown
热门文章
- linux ora 01092,ORA-01173的模拟与故障处理
- unity3d 700种 材质球_温度与耐磨钢球的关系
- 计算机视觉算法工程师 笔试,深度学习算法工程师笔试题目
- java12章_从零开始学Java 第12章 异常处理
- eclipse git提交代码_来看看大厂的Git提交规范,千万别乱提交代码哦...
- Hadoop集群环境搭建之伪分布式搭建流程
- mysql myisam 去掉表锁a_MyISAM表锁的解决方案
- 单模光电转换器怎么接_我对汽车电子电磁兼容测试实验室使用的光电收发器之使用管理方式的一些看法...
- python遍历文件_python遍历目录文件脚本的示例
- substring not found什么意思_英文写作中,除了not,你还会其他否定表达吗?