[Original] About A1, A2, A3 ,..., An calculation of the number of records that can be ranked by n elements in the stack in sequence

Source: Internet
Author: User

In the past, we thought that there was only one possible order of stack entry, namely, 4321, or, because we always thought that the stack was first and later ....., at last, I finally realized that the elements in front of the stack may be out of the stack. For example, before 4 in the stack, 321 has already been out of the stack in sequence, the output stack sequence is 3214. When every element meets the requirements of the first stack, the output stack sequence is 1234. This sequence was the last one I thought, because I have always believed in "advanced and later. I used to be confused about this part. Some time ago, when I wrote a non-recursive first-order traversal algorithm for a binary tree, I realized the importance of the Order of the stack. So I studied it well later, finally, more than half of the problem was solved.

First, let's talk about the main method to solve this problem: metallurgy + recursion.

 

Consider A1, A2, A3 ,..., an has n elements into the stack in sequence (A1, A2, A3 ,..., the n elements are not necessarily ordered, or the n elements can be in any order, but each element must be unique ):

Use S (I, n) to represent "A1, A2, A3 ,..., AI ,..., an is the number of the I-th elements in the n elements of the AI first-out stack, then the elements of the output stack after the AI stack is bound to be {A (I-1), A (I + 1), a (I + 2 ),..., A (I + n-I)} This (n-I + 1) element of a certain element, now first suppose I> 1 TO MAKE A (I-1) meaningful-this is because the n elements are sequentially imported into the stack. Therefore, when Ai First goes out of the stack, it must be any element before AI (that is, the element that comes into the stack before AI) since the stack has been imported and the stack has not been released (because Ai is the first to go out of the stack), The next element to the AI stack must be one of the elements behind the AI (because the elements behind the stack are then added to the stack, so can be any element followed by AI out of stack) or a (I-1), because for the Front (I-1) has been in the stack but has not yet out of the stack element, must wait until a (I-1) out of the stack, A (I-2), A (I-3 ),..., a1 these elements have the opportunity to go out of the stack, said the ground is that a (I-1) blocked the front (I -2) elements!

 

1, for a (I-1) followed by AI out of the stack: A (I-1) is {A1, A2, A3 ,..., A (I-1), A (I + 1), a (I + 2 ),..., an} A total of (n-1) elements of the I-1 element, so in this case all the number of outgoing Stacks is S (I-1, n-1)

2. When a (I + 1) is followed by AI to exit the stack: A (I + 1) is {A1, A2, A3 ,..., A (I-1), A (I + 1), a (I + 2 ),..., an} A total of (n-1) I elements (Note). In this case, the number of all outgoing Stacks is S (I, n-1)

3. When a (I + 2) is followed by AI to exit the stack: A (I + 2) is {A1, A2, A3 ,..., A (I-1), A (I + 1), a (I + 2 ),..., an} A total of (n-1) elements (I + 1) (Note). In this case, the number of all outgoing Stacks is S (I + 1, n-1)

.

.

.

(N-I + 1). For a (I + n-I) [that is, an] And then when ai goes out of the stack: An is {A1, A2, A3 ,..., A (I-1), A (I + 1), a (I + 2 ),..., an} A total of (n-1) elements (n-1), so in this case, the number of all outgoing Stacks is S (n-1, n-1)

 

 

Then, based on the addition principle, we can get:

S (I, n) = S (I-1, n-1) + S (I, n-1) + S (I + 1, n-1) + S (I + 2, n-1) +... + S (I + n-1-i, n-1)

The total number (n-I + 1) is added here.

 

Obviously, when I = 1, there cannot be an element before A1. (did you say that there is a A0 element ?), So for I = 1 to meet the above formula, and in fact it should be S (0, n) = 0, then when I = N, it is obvious that S (n, n) = 1, because the nth element (the last element in the stack) is blocked by the first element, so there is only one arrangement.

 

For the above formula: when the value of I is (I + 1 ),

S (I + 1, n) = S (I, n-1) + S (I + 1, n-1) + S (I + 2, n-1) +... + S (I + n-1-i, n-1)

Total (N-(I + 1) + 1)

 

The two-step subtraction result:

S (I, n)-S (I + 1, n) = S (I-1, n-1) that S (I, n) = S (I-1, n-1) + S (I + 1, n)

In addition, S (0, n) = 0 and S (n, n) = 1

 

So we can calculate S (I, n)

Then there are n elements in A1, A2, A3,..., an which may be arranged as T (n) after they are added to the stack in sequence.

T (n) = S (1, N) + S (2, n) + S (3, n) +... + S (n-1, n) + S (n, n)

Finally, we should find t (N). Unfortunately, in high school, I failed to learn the knowledge of arrangement and combination, so I cannot use the existing mathematical operators to express T (n) or S (I, n), or I cannot find t (n) or S (I, n). If someone can calculate it, please let me know!

 

So I had to write a program to solve this problem, because the computer has a strong computing power!

# Include <iostream> <br/> using namespace STD; </P> <p> // s (I, n) = S (I-1, n-1) + S (I, n-1) + S (I + 1, n-1) +... + S (I + n-1-i, n-1) <br/> // s (I, n)-S (I + 1, n) = S (I-1, n-1) <br/> // s (I, n) = S (I-1, n-1) + S (I + 1, n) </P> <p> int S (int I, int N) // A1, A2, A3 ,..., AI ,..., an the number of all the permutation numbers when the I-th element of the n elements is the first-come stack of AI <br/>{< br/> if (I = 0) // s (0, n) = 0 <br/> return 0; <br/> else if (I = N) // S (n, n) = 1 <br/> return 1; <br/> else <br/> return S (I-1, n-1) + S (I + 1, n ); // s (I, n) = S (I-1, n-1) + S (I + 1, n) <br/>}</P> <p> int T (int n) // A1, A2, A3 ,..., an has n elements in total. The number of possible outgoing stacks after the elements are added to the stack in sequence <br/>{< br/> int sum = 0; <br/> for (INT I = 1; I <= N; I ++) <br/> sum + = S (I, n ); <br/> return sum; <br/>}</P> <p> int factorial (int n) // calculate the full array <br/>{< br/> If (n = 1 | n = 0) <br/> return 1; <br/> return N * fact Orial (n-1); <br/>}</P> <p> int main () <br/>{< br/> int I, j, F, T; <br/> for (I = 1; I <= 13; I ++) <br/> {<br/> for (j = 1; j <= I; j ++) <br/> cout <S (J, I) <''; // outputs the values of S (I, n) first, of course, S (0, n) does not need to be output. <br/> F = factorial (I); // the computation is fully arranged for comparison. <br/> T = T (I ); <br/> cout <"/NT (" <I <") =" <t <'/t' <I <"! = "<F <'/t' // output result <br/> <" t ("<I <") /"<I <"! = "<(Float) T/F <"/n "; // replace T (n) with N! Comparison <br/>}< br/> return 0; <br/>}

The running result is as follows:

1
T (1) = 1 1! = 1 T (1)/1! = 1

1 1
T (2) = 2 2! = 2 T (2)/2! = 1

2 2 1
T (3) = 5 3! = 6 T (3)/3! = 0.833333

5 5 3 1
T (4) = 14 4! = 24 T (4)/4! = 0.583333

14 14 9 4 1
T (5) = 42 5! = 120 t (5)/5! = 0.35

42 42 28 14 5 1
T (6) = 132 6! = 720 T (6)/6! = 0.183333

132 132 90 48 20 6 1
T (7) = 429 7! = 5040 T (7)/7! = 0.085119

429 429 297 165 75 27 7 1
T (8) = 1430 8! = 40320 t (8)/8! = 0.0354663

1430 1430 1001 572 275 110 35 8 1
T (9) = 4862 9! = 362880 T (9)/9! = 0.0133984

4862 4862 3432 2002 1001 429 154 44 9 1
T (10) = 16796 10! = 3628800 T (10)/10! = 0.00462853

16796 16796 11934 7072 3640 1638 637 208 54 10 1
T (11) = 58786 11! = 39916800 T (11)/11! = 0.00147271

58786 58786 41990 25194 13260 6188 2548 910 273 65 11 1
T (12) = 208012 12! = 479001600 T (12)/12! = 0.000434262

208012 208012 149226 90440 48450 23256 9996 3808 1260 350 77 12 1
T (13) = 742900 13! = 1932053504 T (13)/13! = 0.000384513

 

I used it as a result, but unfortunately I cannot upload images. This program passed the test in GCC and vs2008. Can we end recursion, in fact, as long as you pay attention to the changes in I after each recursive call, you can know whether the recursion will end eventually, because 0 <= I <= n, note that this makes it easy to understand that recursion will end, but I still don't want to call a function with invalid data. Make sure that the value is 0 <= I <= n, from the running results, we can see that T (n) is far from n! Because t (n) is only a process of accumulation, even though S (I, n) is called multiple times, therefore, the calculation is very fast (0.133 S ). Note that S (I, n) indicates "A1, A2, A3 ,..., AI ,..., an, the I-th element of the n elements, the number of all the permutation numbers when Ai First goes out of the stack. "This is the key to solving the problem and applying recursion! Now we have a clue to solve this problem. We are ready to write the program again to output all possible output stack sequences.

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.