Title Description:
In the order of,..., N-1,n, ask how many kinds of stack sequences can be obtained. such as n=3 when there are 1 2 3,1 3 2,2 1 3,2 3 1,3 2 1 A total of 5 out of the stack sequence.
Problem Solving Ideas:
Sets the number of scenarios when f (n) is the number of N.
F (0) =1; F (1) =1; F (2) =2; F (3) = 5;
When n=4:
F1 F2 F3 F4
1 2 3 4//Four status marked as F1,f2,f3,f4
If the F1 is located in a position, the number of the number of previous numbers in the stack order is f (0), the number of digits after the number of positions is F (3);
When the F1 is located in a position, the total scheme is F (0) *f (3);
If F1 is located at position second, the number of digits in the position before F1 is known as F (1), and the order of the numbers after position second is F (2).
When F1 is located at position second, the total scheme is F (1) *f (2);
Similarly: F1 at position third when the program number is F (2) *f (1);
F1 at position fourth, the scheme number is F (3) *f (0);
The above conditions can be introduced:
F (4) =f (0) *f (3) +f (1) *f (2) +f (2) *f (1) +f (3) *f (0);
When n is unknown, the above conclusion can be pushed:
F (n) =f (0) *f (n-1) +f (1) *f (n-2) +f (2) *f (n-3) ... f (n-2) *f (1) +f (n-1) *f (0);
The above is the general recursive type of the subject, we will find that the actual time complexity of the equation is O (n^2);
Of course not very soon, so we introduce Cattleya number;
The so-called Cattleya number is when the recursion occurs when h (n) = h (0) *h (n-1) +h (1) *h (n-2) + ... + h (n-1) H (0) (n>=2) ,
The solution of algebraic formula is h (n) =c (2n,n)/(n+1) (n=0,1,2,...) ;
Its alternative recursive type is h (n) =h (n-1) * (4*n-2)/(n+1); I math weak slag concrete how to deduce please find Baidu June
So our formula is reduced to f (n) =f (n-1) * (4*n-2)/(n+1);//priority must not be mistaken priority I've been looking for a long time bug.
Stack order (Cattleya number)