(hdu step 2.3.6)Game of Connections(大数:凸多边形的三角形划分)
来源:互联网 发布:复杂网络系统与动力学 编辑:程序博客网 时间:2024/05/18 21:42
在写题解之前给自己打一下广告哈~。。抱歉了,希望大家多多支持我在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): 443Problem 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
23-1
Sample Output
25
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]);}}}
1 0
- (hdu step 2.3.6)Game of Connections(大数:凸多边形的三角形划分)
- [ACM] hdu 1134 Game of Connections(大数+Catalan数)
- hdu 1134 Game of Connections 【卡特兰数+大数】
- HDU 1134 Game of Connections(Catalan + 大数)
- Game of Connections HDU
- Game of Connections HDU
- HDU 1134 Game of Connections
- HDU 1134 Game of Connections
- HDU 1134 Game of Connections
- HDU 1134 Game of Connections
- HNOI'97凸多边形的三角形划分问题。。
- hdu 1134(大数+卡特兰数——Game of Connections)
- hdu-1134-Game of Connections(卡特兰数 大数乘法除法)
- HDU Game of Connections 解题报告
- hdu/hdoj 1134 Game of Connections
- hdu 1134 game of connections(Catalan Number)
- [Catalan] HDU 1134 Game of Connections
- 高精度-HDU-1134-Game of Connections
- 炼就的Android内功修养
- uva100 3+1
- 括号匹配程序
- 给openwrt添加wifidog组件
- 二月八号之前的计划
- (hdu step 2.3.6)Game of Connections(大数:凸多边形的三角形划分)
- 如何替换HBITMAP中的颜色值 和保存为图片
- ibatis的初相识
- C语言时间差计算函数clock() 与 time ()用法分析
- 关于“三阶幻方”算法的Java实现
- java复习第八课,for循环语句
- Java语言程序设计-基础篇-5.25习题-毫秒转换成时、分、秒
- 使用Spring 内置的JdbcTemplate操作数据库
- 多线程之ThreadGroup