(hdu step 2.3.6)Game of Connections(大数:凸多边形的三角形划分)

在写题解之前给自己打一下广告哈~。。抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下:

http://edu.csdn.net/course/detail/209

题目:

Game of Connections

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

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

题目分析:

这一道题,读完题以后,抽象以下,其模型可以归结为“凸多边形的三角形划分。其中线段两两不相交”。而这一模型又可以使用卡特兰数来解决。

需要注意的是:catalans[20]就已经达到了6564120420。这已经超过了int的范围。catalans[99]的值为227508830794229349661819540395688853956041682601541047340。这已经超过了证书所能表示的范围,所以使用大数来处理

代码如下:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		BigInteger catalans[] = new BigInteger[101];

		catalans[1] = new BigInteger("1");

		BigInteger four = new BigInteger("4");
		BigInteger two = new BigInteger("2");
		BigInteger one = new BigInteger("1");

		int i;
		for(i = 2 ; i <= 100 ; ++i){//注意catalan[20]已经是6564120420了.所以需要用到大数
			//根绝catalans数的递推公式来求得每一项的catalan数
			catalans[i] = catalans[i-1].multiply(four.multiply(BigInteger.valueOf(i)).subtract(two)).divide(BigInteger.valueOf(i+1));
		}

		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			int n = scanner.nextInt();
			if(n == -1){
				return ;
			}

			System.out.println(catalans[n]);
		}
	}
}
时间: 2024-10-21 14:19:27

(hdu step 2.3.6)Game of Connections(大数:凸多边形的三角形划分)的相关文章

(hdu step 2.3.4)How Many Trees?(大数:求n个节点能够成多少棵二叉树)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 976 Accepted Submission(s): 511   Problem

(hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)

题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 168 Accepted Submission(s): 98   Problem Description There are many secret openings in the floor which are covered by a big he

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

(hdu step 1.3.1)FatMouse&#39; Trade(在收入需要一定的付出的情况下求最大收入)

题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5092 Accepted Submission(s): 1530   Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua

(hdu step 6.1.2)Eddy&#39;s picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)

题目: Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 172 Accepted Submission(s): 126   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be

(hdu step 5.1.1)A Bug&#39;s Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)

题目: A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 723 Accepted Submission(s): 277   Problem Description Background Professor Hopper is researching the sexual behavior of a rare spe

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble

(hdu step 3.1.5)Tiling_easy version(求用2*1、2*2两种骨格铺满2*n的网格的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Tiling_easy version Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 515 Accepted Submission(s): 447   Prob

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[