HDU 1134 Game of Connections

38 篇文章 0 订阅
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.通项公式:h(n)=C(n,2n)/(n+1)=(2n)!/((n!)*(n+1)!).

2.递推公式:h(n)=((4*n-2)/(n+1))*h(n-1); h(n)=h(0)*h(n-1)+h(1)*h(n-2)+...+h(n-1)*h(0).

3.前几项为:h(0)=1,h(1)=1,h(2)=2,h(3)=5,h(4)=14,h(5)=42,......

4.应用场景:

a.括号化问题
  矩阵链乘: P=a1×a2×a3×……×an,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?(h(n)种)
b.出栈次序问题
  一个栈(无穷大)的进栈序列为1,2,3,..n,有多少个不同的出栈序列?
  类似:
  (1)有2n个人排成一行进入剧场,入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票,问有多少中方法使得只要有10元的人买票,售票处就有5元的钞票找零?(将持5元者到达视作将5元入栈,持10元者到达视作使栈中某5元出栈)

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

c.将多边形划分为三角形问题
  (1)将一个凸多边形区域分成三角形区域的方法数
  (2)类似:一位大城市的律师在住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?
  (3)类似:在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数
d.给定节点组成二叉树的问题
  给定N个节点,能构成多少种形状不同的二叉树

      (一定是二叉树!先去一个点作为顶点,然后左边依次可以取0至N-1个相对应的,右边是N-1到0个,两两配对相乘,就是

      h(0)*h(n-1) + h(2)*h(n-2) +  + h(n-1)h(0)=h(n))(能构成h(N)个)。

(以上内容来自http://www.cnblogs.com/buptLizer/archive/2011/10/23/2222027.html


2n个人围成一个圆圈,求两两相互握手并且不交叉的所有握手方式。

设2n个人一共有h(n)种,那么现在第一个人可以和第2,4,6,。。。,2(n-1),2n,即必须保证和他握手的那个人两边是偶数。

也就是求卡特兰数。

题目数据范围很大,要用高精乘除~

除的时候注意更新x值要用原来的a[i][j]更新~


#include<cstdio>

int n,a[101][101];

void findd()
{
	a[1][1]=1;
	for(int i=2;i<=100;i++)  //乘4i-2除以i+1  
	{
		int x=0,z=4*i-2;
		for(int j=1;j<=100;j++)
		{
			a[i][j]=a[i-1][j]*z+x;
			x=a[i][j]/10;a[i][j]%=10;
		}
		x=0,z=i+1;
		for(int j=100;j>=1;j--)
		{
			int k=a[i][j];
			a[i][j]=(x*10+k)/z;
			x=(x*10+k)%z;
		}
	}
}

int main()
{
	findd();
	while(scanf("%d",&n)==1 && n!=-1)
	{
		int i=n;
		while(a[n][i]==0) i--;
		for(;i>=1;i--) printf("%d",a[n][i]);printf("\n");
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值