hdoj-1134-Game of Connections【卡特兰数】

Game of Connections

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3599 Accepted Submission(s): 2072

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

Asia 2004, Shanghai (Mainland China), Preliminary

Recommend

Eddy | We have carefully selected several similar problems for you:
1133 1130 1131 2067 1267

#include<stdio.h>
#include<string.h>
const int maxn=200;
int catalan[101][200];
int temp[200];
void make(){
	memset(catalan,0,sizeof(catalan));
	catalan[1][0]=1;
	int i,j,res;
	for(i=2;i<=100;++i){
		int mod=4*i-2;
		for(j=0;j<maxn;++j){
			catalan[i][j]+=catalan[i-1][j]*mod;
			if(catalan[i][j]>=10){
				catalan[i][j+1]=catalan[i][j]/10;
				catalan[i][j]%=10;
			}
		}
		memset(temp,0,sizeof(temp));
		mod=i+1;
		res=0;
		for(j=maxn-1;j>=0;--j){
			temp[j]=(res*10+catalan[i][j])/mod;
			res=(res*10+catalan[i][j])%mod;
		}
		for(j=0;j<maxn;++j){
			catalan[i][j]=temp[j];
		}
	}
}
int main(){
	make();
	int n;
	while(~scanf("%d",&n),n!=-1){
		int i=maxn-1;
		while(!catalan[n][i]) --i;
		while(i>=0){
			printf("%d",catalan[n][i--]);
		}
		printf("\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-12 00:39:18

hdoj-1134-Game of Connections【卡特兰数】的相关文章

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 ord

B - Game of Connections(卡特兰数)

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. E

POJ 2084 Game of Connections 卡特兰数

看了下大牛们的,原来这题是卡特兰数,顺便练练java.递归式子:h(0)=1,h(1)=1   h(n)= h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2)   打表172MS import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new S

HDU 1134 Game of Connections(卡特兰数)

题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4668    Accepted Submission(s): 2729 Problem Description Thi

特殊函数卡特兰数 hdu 1134

Game of Connections Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3352    Accepted Submission(s): 1910 Problem Description This is a small but ancient game. You are supposed to write down the

POJ 2084 Game of Connections(卡特兰数)

卡特兰数源于组合数学,ACM中比较具体的使用例子有,1括号匹配的种数.2在栈中的自然数出栈的种数.3求多边形内三角形的个数.4,n个数围城圆圈,找不相交线段的个数.5给定n个数,求组成二叉树的种数…… 此题就是第4个样例,是裸卡特兰数,但是这里牵扯的大数,可以使用java的大数类解决,但是我这里使用高精度乘法和除法模拟的(主要是java不会). 此处的递推式为H[1] = 1:H[n] = H[n-1]*(4*n-2)/(n+1){n>=2}:代码如下: 需要注意输出的形式,我这里的进制是100

HDOJ 5184 Brackets 卡特兰数扩展

既求从点(0,0)只能向上或者向右并且不穿越y=x到达点(a,b)有多少总走法... 有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1)  /// 折纸法证明卡特兰数: http://blog.sina.com.cn/s/blog_6917f47301010cno.html Brackets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total

hdoj 4828 卡特兰数取模

Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 93    Accepted Submission(s): 25 Problem Description 度度熊近期非常喜欢玩游戏.这一天他在纸上画了一个2行N列的长方形格子. 他想把1到2N这些数依次放进去.可是为了使格子看起来优美,他想找到使每行每列都递增的方案.只是画了

HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won't you? Suppose the cinema only has one ticket-office and

hdoj 1023 Train Problem II 【卡特兰数】

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6928    Accepted Submission(s): 3750 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat