UV-10007 count the trees

Source: Internet
Author: User


Count the trees
Another common social inability is known as ACM (abnormally compulsive meditation ). this psychological disorder is somewhat common among programmers. it can be described as the temporary (although frequent) loss of the faculty of speech when the whole power of the brain is applied to something extremely interesting or challenging.

Juan is a very gifted programmer, and has a severe case of ACM (he even participant ipated in an ACM World Championship a few months ago ). lately, his loved ones are worried about him, because he has found a new exciting problem to exercise his intellectual powers, and he has been speechless for several weeks now. the problem is the determination of the number of different labeled Binary Trees that can be built using exactlyNDifferent elements.

For example, given one elementA, Just one binary tree can be formed (usingAAs the root of the tree). With two elements,AAndB, Four different binary trees can be created, as shown in the figure.

If you are able to provide a solution for this problem, Juan will be able to talk again, and his friends and family will be forever grateful.

Input the input will consist of several input cases, one per line. Each input case will be specified by the number N( 1 ≤ n ≤300) Of different elements that must be used to form the trees. A number 0Will mark the end of input and is not to be processed. output for each input case print the number of Binary Trees that can be built using NElements, followed by a newline character. sample input
Sample output

Question: catlands

Idea: Java trainer

import java.math.BigInteger;import java.util.*;import java.io.*;/** * Created by acer on 14-8-7. */public class Main {public static void main(String[] args) {Scanner cin = new Scanner(System.in);BigInteger[] catalan = new BigInteger[301], fact = new BigInteger[601];int n;fact[0] = BigInteger.ONE;for (int i = 1; i <= 600; i++)fact[i] = fact[i-1].multiply(BigInteger.valueOf(i));for (int i = 0; i <= 300; i++)catalan[i] = fact[i*2].divide(fact[i+1]).divide(fact[i]);while ((n = cin.nextInt()) != 0)System.out.println(catalan[n].multiply(fact[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.