Written algorithm learning--ticket Change question (Cattleya number)

Source: Internet
Author: User
Tags ticket

Question: 2n individuals queue to buy tickets, of which n individuals hold 50 yuan, n individuals hold 100 yuan. Each ticket is 50 yuan, and one person only buys one ticket. At the beginning of the ticket office there was no change. Please tell me how many of these 2n individuals are queued in order not to make the ticket office unable to open the money.

Another form of the topic: there are n pairs of opening and closing parentheses, and now ask exactly how many of the brackets are legally matched by the combination.


Solution:

principle

In fact, this is an application of the Cattleya number:

The first items are: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477 638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452, ...

Make the H (0) =1,h (1) =1,catalan number meet the recursion:

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 (0) =1*2+1*1+2*1=5

Alternative recursion type [2]:

H (N) =h (n-1) * (4*n-2)/(n+1);

The solution of a recursive relationship is:

H (N) =c (2n,n)/(n+1) (n=0,1,2,...)

The alternative solution to the recursive relationship is:

H (N) =c (2n,n)-C (2n,n-1) (n=0,1,2,...)

We now apply h (n) =c (2n,n)/(n+1) (n=0,1,2,...) to solve our change problem (brace matching problem).

We set the closing parenthesis to state ' 1 ' and the left parenthesis to state ' 0 '. The ordering of the 2n number corresponds to the 2n bit binary number consisting of n 1 and N 0. When a closing parenthesis participates in the match, I need to make sure that there is at least one dangling opening parenthesis in the parenthesis sequence before the closing parenthesis , so the total number of feasible solutions equals the 2n bit binary number consisting of n 1 and N 0, which is scanned from the left and right. In this scanning process, the cumulative number of 1 is not less than 0 of the cumulative number of program species.

The number of scenarios in which N 1 is filled in a 2n bit binary number is C (2n,n) (after the position is determined in 1, the position of 0 is automatically determined). In these C (2n,n) scenarios subtract the number of non-conforming requirements (scanned by left and right, 0 of the cumulative number in this scanning process is greater than 1 cumulative), then we get the final solution.

Non-conforming to the requirements of the solution is characterized by a left and right scanning, it is inevitable that an odd digit 2m+1 bit first appears on the cumulative number of M 0 and M+1 1 cumulative number (and the premise, there are M 0,m 1, it is known that the next sequence can also find N-m 0 and n-m-1 1), if this 2m+ 1 bits on the 0 and 1 interchange, making it into m+1 0 and M 1, the result is 1 by n+1 0 and N-1 1 composed of 2n digits, that is, an undesirable number corresponds to a n+1 0 and n-1 a 1 of the arrangement.

In turn, any one consists of n+1 0 and n-1 1 of the binary number of 2n, because 0 of the number of 2, 2n is even, it must be on an odd bit of a 0 cumulative number of more than 1 cumulative number. Also in the preceding section 0 and 1 are interchangeable, so that it is made up of n 0 and N 1 composed of 2n digits, namely n+1 0 and N-1 1 composed of 2n digits must correspond to a non-conforming number.

Thus the non-required 2n digits correspond to the permutation one by one composed of n+1 0,n-1 1.

Clearly, the number of non-conforming schemes is C (2n,n+1). This results in the total number of output sequences = C (2n,n)-C (2n,n+1) = C (2n,n)/(n+1) = h (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.