POJ2084 Game of Connections 卡特兰数 关于卡特兰数经典的几个问题
Posted l609929321
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2084 Game of Connections 卡特兰数 关于卡特兰数经典的几个问题相关的知识,希望对你有一定的参考价值。
Game of Connections
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9128 | Accepted: 4471 |
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?
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.
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
参考博客:
https://blog.csdn.net/sdj222555/article/details/7260739
原理
令h(1)=1,h(2)=1,catalan数满足递归式:卡特兰数的应用
实质上都是递归等式的应用
括号化问题
矩阵链乘: P=a1×a2×a3×……×an,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?(h(n)种)出栈次序问题
一个栈(无穷大)的进栈序列为1,2,3,…,n,有多少个不同的出栈序列?凸多边形的三角剖分问题
求将一个凸多边形区域分成三角形区域的方法数。用给定节点组成二叉树的问题
给定N个节点,能构成多少种不同的二叉树?然后这个题目就是一个裸的卡特兰数,因为到后面爆了long long,所以这里直接用了java的大数
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger [] a = new BigInteger [105]; a[0] = BigInteger.ONE; a[1] = BigInteger.ONE; for( int i = 2; i <= 101; i ++ ) { a[i] = a[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1)); } Scanner cin = new Scanner(System.in); while( cin.hasNext() ) { int n = cin.nextInt(); if( n == -1 ) { break; } System.out.println(a[n]); } } }
以上是关于POJ2084 Game of Connections 卡特兰数 关于卡特兰数经典的几个问题的主要内容,如果未能解决你的问题,请参考以下文章
Game of Taking Stones && POJ1259 /// 最大空凸包 几何+DP
Data source rejected establishment of connection, message from server: "Too many connectio