hdu 题目1134 Game of Connections (卡塔兰数 catalan number)

Game of Connections

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


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
 



百度百科:卡塔兰数列

转载http://blog.csdn.net/jtlyuan/article/details/7440591

卡特兰数:规定C0=1,而C1=1,C2=2,C3=5,C4=14,C5=42,C6=132,C7=429,C8=1430,C9=4862,C10=16796,

C11=58786,C12=208012,C13=742900,C14=2674440,C15=9694845·········································

卡塔兰数的一般项公式为 另类递归式: h(n)=((4*n-2)/(n+1))*h(n-1);

Cn的另一个表达形式为

 

h(n)= h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2)

 

hai可以这样推导出来:

n

推到过程

Cn

1

1

1

2

1 1

2

3

1 2 2

5

4

1 3 5 5

14

5

1 4 9 14 14

42

6

1 5 14 28 42 42

132

7

1 6 20 48 90 132 132

429

···

··· ···

···

 

所以,在做题的时候,我们应该用上面的公式Cn=Ck*Cn-k (k=1,2``n)来判断是否使用于katalan数来解决问题,合适就列出前几项来判断推到出答案

 

总结了一下,最典型的四类应用:(实质上却都一样,无非是递归等式的应用,就看你能不能分解问题写出递归式了)

1.括号化问题

矩阵链乘: P=a1×a2×a3×……×an,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?(h(n)种)

2.出栈次序问题

一个栈(无穷大)的进栈序列为1,2,3,..n,有多少个不同的出栈序列?

类似:有2n个人排成一行进入剧场。入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票,问有多少中方法使得只要有10元的人买票,售票处就有5元的钞票找零?(将持5元者到达视作将5元入栈,持10元者到达视作使栈中某5元出栈)

3.将多边行划分为三角形问题

将一个凸N+2多边形区域分成三角形区域的方法数?

类似:一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她

从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?

类似:在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数?

4.给顶节点组成二叉树的问题

给定N个节点,能构成多少种不同的二叉树?

(能构成h(N)个)

Catalan数的解法

Catalan数的组合公式为 Cn=C(2n,n) / (n+1);

此数的递归公式为 h(n ) = h(n-1)*(4*n-2) / (n+1)

卡特兰数真是一个神奇的数字,很多组合问题的数量都和它有关系,例如:

Cn= n对括号正确匹配组成的字符串数,例如 3对括号能够组成:

((())) ()(()) ()()() (())() (()())

Cn= n+1个数相乘,所有的括号方案数。例如, 4个数相乘的括号方案为:

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

Cn= 拥有 n+1 个叶子节点的二叉树的数量。例如 4个叶子节点的所有二叉树形态:

 

  • Cn=n*n的方格地图中,从一个角到另外一个角,不跨越对角线的路径数,例如, 4×4方格地图中的路径有:

 

  • Cn= n+2条边的多边形,能被分割成三角形的方案数,例如 6边型的分割方案有:

 

hdu 题目1134 Game of Connections (卡塔兰数 catalan number)_第1张图片

  • Cn= 圆桌周围有 2n个人,他们两两握手,但没有交叉的方案数。

 

下面是一些大公司的笔试题

先来一道阿里巴巴的笔试题目:说16个人按顺序去买烧饼,其中8个人每人身上只有一张5块钱,另外8个人每人身上只有一张10块钱。烧饼5块一个,开始时烧饼店老板身上没有钱。16个顾客互相不通气,每人只买一个。问这16个人共有多少种排列方法能避免找不开钱的情况出现。

C8=1430,所以总数=1430*8!*8!

2012腾讯实习招聘笔试题

在图书馆一共6个人在排队,3个还《面试宝典》一书,3个在借《面试宝典》一书,图书馆此时没有了面试宝典了,求他们排队的总数?

C3=5;所以总数为5*3!*3!=180.


/***************************
# 2013-8-28 14:14:08 
# Time: 0MS   Memory: 256KB
# Author: zyh
# Status: Accepted
***************************/ 

#include<stdio.h>
#include<string.h>


int a[110][70];
void catalan()
{
	int n,i,j,len,carry,tmp;
	memset(a,0,sizeof(a));
	a[1][0] = 1;
	len = 1;
	
	for(i=2;i<=100;i++){
		
		for(j=0;j<len;j++) //乘法
			 a[i][j] = a[i-1][j]*(4*i-2);
	 
		 carry = 0;
		 for(j=0;j<len;j++){ //处理相乘结果 
		 	tmp = a[i][j] + carry;
			a[i][j] = tmp%10;
			carry = tmp/10; 
		 }
		 		
	 	while(carry){ //进位处理 
	 		a[i][len++] = carry%10;
	 		carry/=10;
	 	}
		 
	 	carry =0;
	 	for(j=len-1;j>=0;j--){//除法
		 
		 	tmp = carry*10 + a[i][j];
		 	a[i][j] = tmp/(i+1);
		 	carry = tmp%(i+1);
	 	}
	 	
	 	while(!a[i][len-1])	len--;
	} 
}
//896519947090131496687170070074100632420837521538745909320
int main()
{
	int n;
	catalan();
	while(scanf("%d",&n),n!=-1)
	{
		int i =69;
		while(a[n][i]==0) i--;
		while(i>0){
			printf("%d",a[n][i]);
			i--;
		}
		printf("%d\n",a[n][0]);
	}
	return 0;
} 




你可能感兴趣的:(ACM,HDU,特殊的数,卡塔兰数列)