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?
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
题目抽象为凸多边形定点两两连线且互不相交,求方案数。
第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]);
}
}
}

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

357

被折叠的 条评论
为什么被折叠?



