LOJ167 Cantor expansion solution to a problem

Face questions

Cantor unfold:

Cantor is expanded to a full permutation bijection a natural number, commonly used in the construction space hash table compressor. Cantor unfolding essence is to calculate the current arrangement in all small to large full array of rank, and therefore is reversible.

X = A[0] * (n-1)! + A[1] * (n-2)! + … + A[n-1] * 0!

A [i] means that is positioned behind a number less than the number i A [i] value, that is, behind the back of the factorial by number of how many

The calculated value of the number of Hong drag unfold, are all arranged in the order - a value of 1, so that the X- +1 is the order of the full array of

 

Q: Although it is known a method Cantor expanded, but know how to arrange it ranked seek?

A: Then use the inverse Cantor unfold! ;

 

Expand the inverse Cantor:

As mentioned earlier, Hong expansion is dragged from the sequence number of natural and mapping is reversible, then the inverse Kang drag expanded from natural numbers is a sequence of mappings.

for example:

In (1,2,3,4,5) is given can be calculated from 61 to 34,152 permutations
process is as follows:
! With 61/4 2 I = 13, description, described has a number smaller than the first two, so that the first 3.
With 13/3! = 1 2 it, instructions described after the second number is smaller than the second has 2, the second 4 bits.
1/2! = I 01, instructions described after the third bit is not less than the third number, the third bit is 1.
1/1! = I 1 0, instructions described after the second number is less than the fourth bit is 1, the fourth bit 5.

Guess you like

Origin www.cnblogs.com/kamimxr/p/11571579.html