(Hdu step 2.3.6) Game of Connections (large number: triangle division of convex polygon), hdu2.3.6
Make an advertisement for yourself before writing a question ~.. Sorry, I hope you can support my CSDN video courses at the following address:
Http://edu.csdn.net/course/detail/209
Question:
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 DescriptionThis is a small but failed ent 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? |
InputEach line of the input file will be a single positive number n, cannot t the last line, which is a number-1. You may assume that 1 <=n <= 100. |
OutputFor each n, print in a single line the number of ways to connect the 2n numbers into pairs. |
Sample Input23-1 |
Sample Output25 |
|
SourceAsia 2004, Shanghai (Mainland China), Preliminary |
RecommendEddy |
Question Analysis:
After reading this question, the model can be abstracted as "triangle division of a convex polygon. The line segments do not overlap ". This model can be solved by the catlan number.
Note that catalans [20] has reached 6564120420. This is beyond the int range. The value of catalans [99] Is 227508830794229349661819540395688853956041682601541047340. This is beyond the scope of the certificate, so use a large number to process
The Code is as follows:
Import java. math. bigInteger; import java. util. extends; 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) {// note that catalan [20] is already 6564120420. therefore, the recursive formula of the number of catalans must be used to obtain the catalan number of each item. catalans [I] = catalans [I-1]. multiply (four. multiply (BigInteger. valueOf (I )). subtract (two )). divide (BigInteger. valueOf (I + 1);} partition = new partition (System. in); while (partial. hasNext () {int n = seconds. nextInt (); if (n =-1) {return;} System. out. println (catalans [n]) ;}}