[Java Exercise 7 -- number of Catalan] poj 2084

Source: Internet
Author: User

Practice: first fix a point such as 1, and then connect 1--3, 1-4 ......., in this way, there are F1 and F2 conditions on both sides of the Line Segment, so there is a formula F (n) = f (0) * F (n-2) + F (2) * F (n-4) + f (4) * F (n-8 )..... + f (n-2) * F (0), a look, this is the famous catalan number!

Make H (1) = 1, h (0) = 1, and the catalan number meets the recursive formula:

H (n) = H (0) * H (n-1) + H (1) * H (n-2) +... + H (n-1) H (0) (N> = 2)

Alternative recursion:

H (n) = H (n-1) * (4 * N-2)/(n + 1); // I use this formula!

The recursive relationship is resolved as follows:

H (n) = C (2n, N)/(n + 1) (n = 1, 2, 3 ,...)

Import Java. io. *; import Java. math. *; import Java. util. *; public class main {public static void main (string [] ARGs) {int N; partition CIN = new partition (New bufferedinputstream (system. in); While (CIN. hasnext () {n = cin. nextint (); If (n =-1) break; biginteger A [] = new biginteger [110]; int I; A [0] = A [1] = new biginteger ("1"); for (I = 2; I <= N; I ++) {A [I] = A [I-1]. multiply (biginteger. valueof (4 * I-2); a [I] = A [I]. divide (biginteger. valueof (I + 1);} system. out. println (A [n]) ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.