The eight-digit problem does not require Cantor expansion and is repeated for 8 seconds, and the Cantor expansion is used for 30 MS. The biggest and most obvious role of Cantor expansion is in determining whether the state is repeated. It is actually a hashing technique.
1. Cantor expansion
[Problem Background] For a set {1,2,3,4,...,n} with n different elements, the total arrangement is sorted from small to large (the same goes from large to small). Obviously it has n! item. If n=4, then there are 4! =4×3×2×1=24 items.
With the natural numbers 1, 2, 3, 4, -----n! One-to-one correspondence. For example, the complete arrangement of the four numbers 1 to 4 is as follows in dictionary order:
1234: 1st 1243: 2nd 1324: 3rd 1342: 4th 1423: 5th 1432: 6th |
2134: The 7th 2143: The 8th 2314: The 9th 2341: The 10th 2413: The 11th 2431: The 12th |
3124: The 13th 3142: The 14th 3214: The 15th 3241: The 16th 3412: The 17th 3421: The 18th |
4123: The 19th 4132: The 20th 4213: The 21st 4231: The 22nd 4312: The 23rd 4321: The 24th |
【main problem】
Example 1: What is the order of 4132? Looking at the above, you will know that the answer is: 20. So how is it calculated?
Solution: There are 4 numbers in total, so n=4.ans:=0;
The first number is 4. There are three numbers smaller than 4 that have not appeared before: 1, 2, and 3. Then ans:=ans+ 3*(n-1)!
So ans:= ans+ 3* 3*2*1 =18
The second number is 1, and the number that is smaller than 1 and has not appeared before is 0. Then ans:=ans+ 0 * (n-2)!, then ans remains unchanged.
The third number is 3. The number smaller than 3 and has not appeared before is 1:2. Then ans:=ans+ 1* (n-3)!, then ans:=18+1* 1=19
The fourth number is 2. The number that is smaller than 2 and has not appeared before is 0. Then ans remains unchanged. In fact, we don’t need to study the last one. All the ones bigger and smaller have appeared before. Why is ans equal to 19 in the end? ? It means there are 19 permutations in front of it, then 4132 itself is the 20th number (last ans:=ans+1)
Example 2: What is the order of 45231?
4 5 2 3 1
ans:= 3*4! + 3*3! + 1*2! + 1*1! + 0*0! + 1 =94
practise
Question 1: Find the position of 35142 in the order from 1 to 5 from smallest to largest?
Question 2: Find the position of 64827315 in the arrangement from small to large from 1 to 8?
Question 3: Find the position of 35142 in the order from big to small from 1 to 5?
Question 4: Find the position of 64827315 in the arrangement from big to small from 1 to 8?
2. Inverse operation of Cantor expansion
I changed the previous div to /. div is the arithmetic symbol of Pascal language, and / is C++. The meaning is the same, which is to find the quotient of two integers after division (ignoring the remainder).
Example 3: Find the 96th permutation among all the arrangements of 1 to 5 from smallest to largest?
Solution: First, suppose x1x2x3x4x5, (x1 is equal to? Don’t know), use 96-1 to get 95, which means there are 95 sequences in front of x1x2x3x4x5.
The first number x1, assuming that x1 currently has k numbers that are smaller than x1 and have not appeared before, then
k:= 95 / (n-1)! = 95 / 24=3, that is, there are 3 numbers that are smaller than x1 and have not appeared before, then x1=4.
95 becomes 95-3×24=23
The second number x2, assuming that x2 currently has k numbers that are smaller than x2 and have not appeared before, then
k:= 23 / (n-2)! = 23 / 6 = 3, that is, there are 3 numbers smaller than x2 that have not appeared before, then x2=5. (There are 3 numbers smaller than it, which is 4 , but 4 has already appeared before, so it is 5)
23 becomes 23 – 3 * 6 = 5
Divide 2 by 5 for the third number! You get 2 with a remainder of 1. Then the third number is the third smallest number 3 that has not yet appeared (5 becomes 1)
Divide 1 by 1 for the fourth number! You get 1 with a remainder of 0. Then the fourth number is the second smallest number 2 that has not yet appeared.
The fifth number is the one that has not appeared at the end.
So the number is 45321
practise:
Question 5: Sort the 8 numbers from small to large. Find the 28017th arrangement?
Solution: First, suppose x1x2x3x4x5x6x7x8, (x1 is equal to? Don’t know), use 28017-1 to get 28016, which means there are 28016 sequences in front of x1x2x3x4x5x6x7x8.
The first number x1, assuming that x1 currently has k numbers that are smaller than x1 and have not appeared before, then k= 28016 / (8-1)! = 28016 / 5040=5 (28016 divided by 5040 is equal to 5, not Logical remainder), that is, there are 5 numbers that are smaller than x1 and have not appeared before, then x1=6.28016 becomes 28016 - 5040*5=2816
The second number x2, assuming that x2 currently has k numbers that are smaller than x2 and have not appeared before, then k= 2816/ (n-2)! = 2816 / 720 = 3, that is, there are 3 smaller numbers than x2 And there is no number that has appeared before, then x2=4.2816 becomes 2816– 3 * 720 = 656
The key is to understand the third number and why it is 8.
The third number x3, assuming that x3 currently has k numbers that are smaller than x3 and have not appeared before, then k= 656/ (n-2)! = 656 / 120 = 5, that is, there are 5 smaller numbers than x3 And there is no number that has appeared before, then x3=? .
1 (has not appeared yet)
2 (has not appeared yet)
3 (has not appeared yet)
4 (occurred)
5 (has not appeared yet)
6 (appeared)
7 (has not appeared yet)
8 (has not appeared yet)
1, 2, 3, 5, 7 (these five have not appeared yet), so the answer is x3=8
by scy