HDU-1023 train Problem II.

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1023

Question:

Give you a number of N, which indicates that there are n trains numbered from 1 to n. They are driving past the distance and ask you how many outbound possibilities are there.

Solution:

Simulate stack issues.

In fact, this question is also a typical application of the combination of math catlands. The following describes the catlan numbers.

========================================================== ====================================

Catlands

Catlan numbers, also known as cataran numbers, are a series that often appear in today's various counting problems in composite mathematics. Named by the Belgian mathematician oryn Charlie katarand (1814-1894.

The catlan formula is widely used. The most typical four application problems are described as follows:

1. brackets.

Matrix chain multiplication: P = A1 × a2 × A3 × ...... ×An: according to the law of multiplication, without changing the order, we only use parentheses to represent the product of pairs. How many parentheses are there? (H (n)

2. Out-of-stack order problems.


How many different out-of-stack sequences are there for a stack (infinite) of 1, 2, 3,... n?


Similarly, 2n people entered the theater in a row. Admission Fee: 5 yuan. Only n of them have a 5-yuan bill, N others only have 10-yuan bills, and the theater does not have any other money. How many ways do they have to buy a ticket for only 10 yuan, is there a change of 5 yuan worth of money at the ticket office? (The arrival of 5 yuan is regarded as 5 yuan into the stack, and the arrival of 10 yuan is regarded as a 5 yuan out of the stack)


3. Divide multilateral rows into triangles.


Number of methods for dividing a convex polygon area into triangle areas?


Similarly, a lawyer in a big city works in N blocks north of her residence and N blocks east of her residence. Every day, she goes to work in 2n blocks. Suppose she never crosses (but can touch) the diagonal line from home to office, how many possible roads are there?


Similarly: Select 2n points on the circle, and connect these points into pairs to make the resulting N line segments non-intersecting by the number of methods?


4. Create a binary tree for the top node.


How many different binary trees can a given n nodes constitute?

Catalan Number Solution


1. The combination formula of Catalan numbers is Cn = C (2n, N)/(n + 1 );
2. the recursive formula for this number is H (n) = H (n-1) * (4 * N-2)/(n + 1 ).
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)
For example, H (2) = H (0) * H (1) + H (1) * H (0) = 1*1 + 1*1 = 2
H (3) = H (0) * H (2) + H (1) * H (1) + H (2) * H (1) = 1*2 + 1*1 + 2*1 = 5
Alternative recursion:
  H (n) = H (n-1) * (4 * N-2)/(n + 1 );
The recursive relationship is resolved as follows:
  H (n) = C (2n, N)/(n + 1) (n = 1, 2, 3 ,...)

Other exam this post, very good summary: http://blog.163.com/lz_666888/blog/static/1147857262009914112922803/


The Code is as follows (Java is used for the first time ):

import java.io.*;import java.util.*;import java.math.BigInteger;public class Main{public static void main(String args[]){BigInteger[] a = new BigInteger[101];a[0] = BigInteger.ZERO;a[1] = BigInteger.valueOf(1);for(int i = 2; i <= 100; ++i)a[i] = a[i - 1].multiply(BigInteger.valueOf(4 * i - 2)).divide(BigInteger.valueOf(i+1));Scanner in = new Scanner(System.in);int n;while(in.hasNext()){n = in.nextInt();System.out.println(a[n]);}}}


HDU-1023 train Problem II.

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.