hdu1134(卡特兰数)

本文探讨了一种经典的组合数学问题,即如何计算在一个由2n个连续编号节点组成的圆圈中,按照特定规则进行配对连线的方法总数。该问题通过递归公式与卡特兰数相结合来解决。

Game of Connections

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


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   |   We have carefully selected several similar problems for you:   1133  1130  1267  3398  1117 
         题目抽象为凸多边形定点两两连线且互不相交,求方案数。
       第1个点只能和偶数下表的点相连,不管怎样,多边形被分成两部分。f[n]=∑f[i]*f[n-1-i];卡特兰数模型。
import java.math.BigInteger;
import java.util.Scanner;


public class hdu1134 {
	
	static BigInteger []f=new BigInteger[110];
	static void catlan()
	{	
		f[0]=new BigInteger("1");
		f[1]=new BigInteger("1");
		for(int i=2;i<101;i++)
		{
			f[i]=new BigInteger("0");
			for(int j=0;j<i;j++)
			{
				f[i]=f[i].add( f[j].multiply(f[i-j-1]) );
			}
		}
	}
	
	
	public static void main(String[] str)
	{
		Scanner input=new Scanner(System.in);
		catlan();
		int i;
		while(input.hasNext())
		{
			i=input.nextInt();
			if(-1==i)break;
		   System.out.println(f[i]);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值