Pop sequence statistics

Description The stack is a commonly used data structure. There are n elements on the top side of the stack waiting to be pushed into the stack, and the other side of the top of the stack is the pop sequence. You already know that there are two types of stack operations: push and pop, the former is to push an element onto the stack, and the latter is to pop the top element of the stack. Now to use these two operations, a series of output sequences can be obtained from a sequence of operations. Please program to find out for a given n, calculate and output the total number of output sequences that may be obtained by a series of operations from the operand sequence 1, 2, ..., n. The input is a number n (1≤n≤15). Output a number, the total number of possible output sequences. sample input

3

Sample output

5

Tip: First understand the two basic operations of the stack, push is to put the element on the top of the stack, the pointer on the top of the stack is moved up one place, and the queue waiting to be pushed is also moved up one place. Pop is to pop the element on the top of the stack. At the same time, the top of the stack pointer is moved down one bit.
  Using a process to simulate the process of entering and exiting the stack, backtracking can be achieved by looping and recursion: repeating such a process, if it can be pushed into the stack, an element will be inserted, and if it can be popped, an element will be ejected. Just try one by one, and count once when the number of popped elements reaches n (this is also the condition for the end of the recursive call).

#include <iostream>  
using namespace std;
 
intmain()  
{  
    int n;
    cin>>n;
    int years = 1;  
    
    for(int i=1; i<=n; i++)  
        years = 2 * (2 * (i - 1) + 1) * years/ (i + 1);  
  
   cost<<years;  
  
    return 0;  
}  

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

Applications of Cattleya Numbers

 The formula for calculating the Cattelan number is:C_0 = 1 /quad , /quad C_{n+1}=/frac{2(2n+1)}{n+2}C_n

 

Application 1: Pop order. The push sequence of a stack (infinity) is 1, 2, 3, ..., n. How many different pop sequences are there? h(n)
General analysis
First, we set f(n) = the number of sequences is n Number of pop sequences. At the same time, we assume that from the beginning to the first time the stack is empty, the ordinal number of the first pop in this process is k. In particular, if the stack is not empty until the end of the entire process, then k=n. The ordinal k of the first popping before the first popping divides the sequence 1~n into two sequences, one of which is 1~k-1, and the number of sequences is k-1, and the other is k+1~n, the sequence The number is nk. At this time, if we regard k as determining an ordinal number, then according to the multiplication principle, the problem of f(n) is equivalent to - the number of popped sequences with the number of sequences k-1 multiplied by the number of sequences is n - The number of pop-up sequences for k, that is, f(n)=f(k-1)×f(nk) for selecting the ordinal number of k. And k can be selected from 1 to n, so according to the principle of addition, the number of sequences with different values ​​of k is added, and the total number of sequences obtained is: f(n)=f(0)f(n-1)+ f(1)f(n-2)+...+f(n-1)f(0). Seeing this, let's look at the recurrence formula of Cattelan number. The answer is self-evident, that is, f(n)=h(n)= C(2n,n)/(n+1)= c(2n ,n)-c(2n,n+1)(n=0,1,2,...). Finally, let f(0)=1 and f(1)=1.
Unconventional analysis
For each number, it must be pushed once and popped once. We set the push to state '1' and the pop to state '0'. All states of n numbers correspond to 2n-bit binary numbers consisting of n ones and n zeros. Since the operands waiting to be pushed onto the stack are arranged in the order of 1‥n, and the operand b pushed onto the stack is greater than or equal to the operand a popped from the stack (a≤b), the total number of output sequences = scan from left to right by n A 2n-bit binary number composed of 1 and n 0s, and the number of schemes for the cumulative number of 1s not less than the cumulative number of 0s. The number of schemes for filling n 1s in a 2n-bit binary number is c(2n,n), and the remaining n bits that are not filled with 1s are automatically filled with 0s. The number of solutions that do not meet the requirements (scanning from left to right, the cumulative number of 0 is greater than the cumulative number of 1) is subtracted from it.
The characteristic of the number that does not meet the requirements is that when scanning from left to right, the cumulative number of m+1 0s and the cumulative number of m 1s must first appear on an odd bit 2m+1, and then 2(nm) There are nm 1s and nm-1 0s on the -1 bit. If the 0s and 1s on the latter 2(nm)-1 bits are interchanged to make them nm 0s and nm-1 1s, the result is 1 composed of n+1 0s and n-1 1s 2n digits, that is, an undesirable number corresponds to a permutation of n+1 zeros and n-1 ones. Conversely, for any 2n-bit binary number composed of n+1 0s and n-1 1s, since the number of 0s is 2 more, and 2n is an even number, there must be a cumulative number of 0s in an odd-numbered digit. Cumulative number over 1. Also in the latter part, 0 and 1 are swapped to make it a 2n-digit number composed of n 0s and n 1s, that is, the 2n-digit number composed of n+1 0s and n-1 1s must correspond to one that does not meet the requirements. number of. Therefore, the undesirable 2n digits correspond to the permutation of n+1 0s, n-1 1s.
Obviously, the number of schemes that do not meet the requirements 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).

Application 2 Description: How many ways can n pairs of parentheses be matched? h(n)
       idea: n pairs of parentheses are equivalent to 2n symbols, n left parentheses, n right parentheses, and the solution of the problem can be set as f(2n). The 0th symbol must be the left parenthesis, and the matching right parenthesis must be the 2i+1th character. Because if it is the 2i-th character, then the 0-th character and the 2i-th character contain an odd number of characters, and an odd number of characters cannot constitute a match.
       Through simple analysis, f(2n) can be transformed into the following recursion f(2n) = f(0)*f(2n-2) + f(2)*f(2n - 4) + ... + f( 2n - 4)*f(2) + f(2n-2)*f(0). To explain briefly, f(0) * f(2n-2) means that the 0th character matches the 1st character, and the remaining characters are divided into two parts, one part is 0 characters, and the other part is 2n-2 characters, Then solve the two parts. f(2)*f(2n-4) means that the 0th character matches the 3rd character, and the remaining characters are divided into two parts, one part is 2 characters, and the other part is 2n-4 characters. And so on.
       Assuming f(0) = 1, calculate the first few terms, f(2) = 1, f(4) = 2, f(6) = 5. Combined with the recursion, it is not difficult to find that f(2n) is equal to h(n).
Description of application 3: Matrix chain multiplication : P=a1×a2×a3×…×an, according to the associative law of multiplication, without changing its order, only parentheses are used to represent paired products. How many parenthesis schemes are there? The idea of ​​h(n-1)
       : It can be considered in this way. First, by bracketing, divide P into two parts, and then bracket the two parts respectively. For example, it is divided into (a1)×(a2×a3.....×an), and then parentheses (a1) and (a2×a3.....×an) respectively; another example is divided into (a1×a2) ×(a3.....×an), then parentheses (a1×a2) and (a3.....×an).
       Let the number of bracketing schemes for n matrices be f(n), then the solution to the problem is
        f(n) = f(1)*f(n-1) + f(2)*f(n-2) + f(3)*f(n-3) + f(n-1)*f(1). f(1)*f(n-1) means that it is divided into two parts (a1)×(a2×a3.....×an), and then bracketed respectively.
       Calculate the first few terms, f(1) = 1, f(2) = 1, f(3) = 2, f(4) = 5. Combined with the recursion, it is not difficult to find that f(n) is equal to h(n-1).
Application 4 description (same as application 1) : The push sequence of a stack (infinity) is 1, 2, 3, ..., n, how many different pop sequences are there? h(n)
       idea: This is the same as the parenthesized one Very similar, the push operation is equivalent to the left parenthesis, and the pop operation is equivalent to the right parenthesis. The push and pop order of n numbers form a sequence of 2n numbers. The 0th number must be the number that is pushed into the stack, and the number that is popped from the stack must be the 2i+1th number. Because if it is 2i, then there is an odd number in the middle, and these odd numbers definitely cannot constitute a stacking and popping sequence.
       Let the solution of the problem be f(2n), then f(2n) = f(0)*f(2n-2) + f(2)*f(2n-4) + f(2n-2)*f(0 ). f(0) * f(2n-2) means that the 0th number is popped from the stack immediately after it is pushed into the stack. At this time, the number of numbers contained between the push and pop of this number is 0, and the remaining number is 2n-2. f(2)*f(2n-4) means that the 0th number contains 2 numbers between the stack and the pop, which is equivalent to 1 2 2 1, and the rest is 2n-4 numbers. And so on.
       Assuming f(0) = 1, calculate the first few terms, f(2) = 1, f(4) = 2, f(6) = 5. Combined with the recursion, it is not difficult to find that f(2n) is equal to h(n).
Description of Application 5: How many situations are there in a binary tree composed of n nodes? h(n)
       Idea: It can be considered in this way, the root will definitely occupy a node, then the remaining n-1 nodes can have the following allocation methods, T(0, n-1), T(1, n-2), .. .T(n-1, 0), let T(i, j) denote that the left subtree of the root contains i nodes, and the right subtree contains j nodes.
       Let the solution of the problem be f(n), then f(n) = f(0)*f(n-1) + f(1)*f(n-2) + .......+ f( n-2)*f(1) + f(n-1)*f(0). Suppose f(0) = 1, then f(1) = 1, f(2) = 2, f(3) = 5. Combining the recurrence formula, it is not difficult to find that f(n) is equal to h(n).
Application 6 Description: Number of ways to select 2n points on a circle and connect these points in pairs such that the resulting n line segments do not intersect ? h(n)
       idea: take one of the points as the base point, number 0, and then number the other points clockwise. Then the number of the point connected to the number 0 must be an odd number. Otherwise, if there are an odd number of points between the two numbers, a point is bound to be isolated, that is, there is an isolated point on both sides of a line segment, resulting in two line segments. intersect. Suppose the selected base point is A, and the point connected to it is B, then A and B divide all points into two parts, one part is located on the left side of A and B, and the other part is located on the right side of A and B. Then solve the two parts separately.
       Let the solution of the problem f(n), then f(n) = f(0)*f(n-2) + f(2)*f(n-4) + f(4)*f(n-6) + ......f(n-4)*f(2) + f(n-2)*f(0). f(0)*f(n-2) means that the point number 0 is connected to the point number 1, and the number of points to the right of them is 0, and the points to the left of them are 2n-2. And so on.
       f(0) = 1, f(2) = 1, f(4) = 2. Combined with the recursion, it is not difficult to find that f(2n) is equal to h(n).
Application 7 Description: Find the number of ways to divide a convex polygonal region into triangular regions?The idea of ​​h(n-2)
      : take one side of the convex polygon as the base, and set the two vertices of this side as A and B. Select 1 of the remaining vertices to divide the convex polygon into three parts, a triangle in the middle, and two convex polygons on the left and right sides, and then solve the left and right convex polygons.
      Let the solution of the problem f(n), where n is the number of vertices, then f(n) = f(2)*f(n-1) + f(3)*f(n-2) + ..... .f(n-2)*f(3) + f(n-1)*f(2). f(2)*f(n-1) means that three adjacent vertices form a triangle, then the number of vertices of the other two parts is 2 and n-1 respectively.
      Let f(2) = 1, then f(3) = 1, f(4) = 2, f(5) = 5. Combining the recursion, it is not difficult to find that f(n) is equal to h(n-2).
Application 8 Description: There are 2n people lined up to enter the theater . Admission fee is 5 yuan. Among them, only n people have a 5-yuan bill, and the other n people only have 10-yuan bills, and there are no other bills in the theater. How many ways are there so that as long as there are 10-yuan people who buy a ticket, the ticket office will have a 5-yuan bill to change? h(n)
     idea: Buying a ticket with 5 yuan can be regarded as entering the stack, then buying a ticket with 10 yuan is regarded as an exit of 5 yuan. This problem is transformed into the pop order number of the stack. As a direct result from the analysis of application 2, f(2n) is equal to h(n).

 

PS application 8: One ticket for the amusement park is 1 yuan, and each person is limited to one. Now there are 10 children lining up to buy tickets, 5 of them only have a 1-yuan bill each, and the other 5 children have only a 2-yuan bill each. The conductor did not prepare any change. Q: How many ways are there to queue so that the conductor can always find change?

Answer: Now, the 5 children who take 1 yuan are regarded as the same, and the 5 children who take 2 yuan are also regarded as the same, using our commonly used "point-by-point accumulation method":

Each small horizontal segment in the figure represents a child who takes 1 yuan, and each small vertical segment represents a child who takes 2 yuan. It is required that any point in the grid from A to B has a number of horizontal segments not less than the number of vertical segments: take 1 yuan must be first, and the number of people cannot be less than 2 yuan, that is, it cannot cross the diagonal AB: the number marked at each point is the number of ways to get from A to this point. Find the number of ways to move from A to B. Point-by-point accumulation can be calculated as 42, that is, Cattelan number C 5 =42.


And because each child is different, there are 42×5 in total! ×5! =42×120×120=604800 cases.

If there are 10 people in this question, 5 people get 1 yuan, 5 people get 2 yuan, and there are 2n people in total, n people get 1 yuan, and n people get 2 yuan, then the number of queuing methods that meet the requirements is: n!*n!*h(n)=n!*n!*C(2n,n)/(n+1)=(2n)!/(n+1)

Examples of Cattelan numbers:

There are many combinatorial structures in combinatorics that can be counted by Catalan numbers. The exercises in Richard P. Stanley's book Enumerative Combinatorics: Volume 2 include 66 distinct combinatorial structures that can be represented by Catalan numbers. Here are some examples with Cn=3 and Cn=4:
Example 1. Cn represents the number of dyck words of length 2n. Dyck word is a string consisting of n Xs and n Ys, and all prefix strings satisfy that the number of Xs is greater than or equal to the number of Ys. The following are dyck words of length 6: h(3)=5
XXXYYY XYXXYY XYXYXY XXYYXY XXXYYY
Example 2. Replace X with a left parenthesis and Y with a right parenthesis in the above example, and Cn represents all legal expressions containing n groups of parentheses The number of: h(3)=5
((())) ()(()) ()()() (())() (()())
Example 3. Cn means that there are n nodes The number of alternatives for a binary tree of different configurations. In the figure below, n equals 3, circles represent nodes, and crescents represent nothing. h(3)=5


Example 4. Cn represents the number of all monotonic paths that do not cross the diagonal in the n × n lattice. A monotonic path starts from the lower left corner of the grid point and ends in the upper right corner of the grid point, with each step up or to the right. Counting the number of such paths is equivalent to counting the number of Dyck words: X for "right" and Y for "up". The following figure shows the case of n = 4: h(4)=14

Similar: A large city attorney works n blocks north and n blocks east of her residence. Every day she walks 2n blocks to work. If she never crosses (but can hit) the diagonal line from home to office, how many possible paths are there? h(n)



Example 5. Cn represents the number of ways to divide an n-sided convex polygon into triangles by connecting vertices. The following figure shows the case of n=6: h(n-2)=h(4)=14


Example 6. Cn represents the number of ways to fill a stepped figure with a height of n with n rectangles. The following figure shows the case of n = 4: h(4)=14


Example 7. The problem of putting in and out of the stack:
After the meal, the elder sister washes the dishes, and the younger sister puts the bowls washed by the elder sister into the cupboard one by one into a pile. There are a total of n different bowls, and they are also stacked in a pile before washing. Maybe because the younger sister is too playful, the bowls are not put into the cupboard in time. How many possible ways are there for the bowl? h(n)
A convoy of cars is driving on a narrow road, not allowed to overtake, but can enter a cul-de-sac to refuel, and then cut in line. There are n cars in total. How many different ways are there to make the convoy go out of town? h(n)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325088746&siteId=291194637