[Beauty of programming] buy a ticket to change to zero

Source: Internet
Author: User

I. Problem: N people hold 1 yuan, and N people take 2 yuan to buy tickets. The fare is one yuan, and the ticket yuan can only be used to buy one yuan from N people for the change of 2 yuan. There are several ways to arrange

Analysis: catlan Number Method

Recursive Formula: F (2 * n) = f (0) * F (2 (n-1) + F (1) * F (2 (n-2) + ...... + F (2 (n-1) * F (0)

F (n) = f (0) * F (n-1) + F (1) * F (n-2) + ...... + F (n-1) * F (0)

Answer:

Number of all sequences: C (2n, n)

Number of invalid sequences: assume there is a sequence of N-1 1 yuan, n + 1 2 yuan. Number of such sequences: C (2n, n-1)

If K exists, so that the number of 1 in this sequence is 1 less than 2, replace all 1 after it with 2, and replace all 2 with 1. Then the sequence has n 1 and N 2. In this case, this sequence (N-1 1, n + 1 2) corresponds one to one with (N 1, N 2. Therefore, the number of invalid sequences is: C (2n, n-1)

So the number of valid sequences: C (2n, n)-C (2n, n-1) = C (2n, N)/(n + 1)

Ii. Expansion problem: how many different binary trees can be constructed by N nodes?

Analysis:

When n = 1, there is only one root node, then only one form of binary tree can be formed, so that the number of Binary Trees that can be composed of N nodes is represented as H (N ), H (1) = 1; H (0) = 0;

When n = 2, one root node is fixed and there are 2-1 nodes. This node can be divided into two groups: (1, 0) and (0, 1. That is, put one on the left, 0 on the right, 0 on the left, and 1 on the right. That is, H (2) = H (0) * H (1) + H (1) * H (0) = 2, which can form two binary trees.

When n = 3, one root node is fixed and there are two nodes. The two nodes can be divided into three groups: (2, 0), (1, 1), (0, 2. That is, H (3) = H (0) * H (2) + H (1) * H (1) + H (2) * H (0) = 5, it can form five binary trees.

Similarly, when n> = 2, the number of Binary Trees that can be composed is H (n) = H (0) * H (n-1) + H (1) * H (n-2) +... + H (n-1) * H (0), that is, it complies with the definition of the Catalan number and can be obtained directly using the general formula.

Let H (1) = 1, h (0) = 1, catalan number (catlan number) meet 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) = (4 * N-2)/(n + 1) * H (n-1 );

The recursive relationship is resolved as follows:

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

Extension:

The Application of catlan numbers (essentially the application of recursive equations)

1. Brackets problem matrix multiplication: P = A1 × a2 × A3 × ...... ×An: According to the multiplication combination law, without changing its order, we only use parentheses to represent the product of pairs. How many methods are there to enclose? (H (n)

2. Out-of-stack order problems a stack (infinite) stack feed sequence is 1, 2, 3 ,..., N, how many different sequence of output stacks?

Analysis
For each number, it must be pushed to and out of the stack once. We set the status of the incoming stack to '1' and the outbound stack to '0 '. All States of N numbers correspond to a 2n binary number consisting of n 1 and N 0. The number of operations waiting for the inbound stack is arranged in the order of 1 Gb/s n. The number B of the inbound stack is greater than or equal to the value a (A ≤ B) of the outbound stack ), therefore, the total number of output sequences = the number of 2n binary numbers consisting of n 1 and N 0 from the left and right scans. The cumulative number of 1 is not less than 0.

In the 2n-bit binary number, the number of N 1 values is C (2n, n), and the remaining N values of 1 are automatically set to 0. The number of solutions that subtract the number of non-conformities (from left to right scans, and the number of accumulative counts of 0 is greater than 1) is required.

The number of non-conformities is characterized by left-to-right scans, in a certain odd digit 2 m + 1, the cumulative count of m + 1 0 and the cumulative count of M 1 will first appear, and the subsequent 2 (n-m) -1 bit has n-M 1 and n-m-1 0. If the next 2 (n-m)-1 on the 0 and 1 swap, making it n-M 0 and n-m-1 1, result 1 2n digits consisting of n + 1 0 and N-1 1 are obtained, that is, an undesired number corresponds to an arrangement consisting of n + 1 0 and N-1 1.

In turn, any 2n binary number consisting of n + 1 0 and N-1 1, because the number of 0 is two more, 2n is an even number, therefore, the cumulative count of 0 must be greater than 1 on a certain odd digit. Similarly, 0 and 1 are swapped in the following parts to form a 2n digit consisting of n 0 and N 1, that is, the 2N digits of N + 1 0 and N-1 1 must correspond to an invalid number.

Therefore, the noncompliant 2n digits correspond to n + 1 0 and N-1 1 in a one-to-one arrangement.

Obviously, the number of non-conforming solutions is C (2n, n + 1 ). The total number of output sequences = C (2n, n)-C (2n, n + 1) = 1/(n + 1) * C (2n, n ).

(The subscript of this formula starts with H (0) = 1)

Similar problems

There are 2n people in a row into the theater. Admission Fee: 5 yuan. Only n people have a 5-yuan bill, N others have only 10-yuan bills, and the theater has no other money. How many ways can one buy a ticket if 10 yuan is needed, 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. Calculate the number of methods for dividing a convex polygon area into a triangle area.

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. If she never cross (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?

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.