Cantor Expands Study Notes

Introduce:

  For an arrangement of 1 ~ n, if we want to save it as a state, we usually open an n-dimensional array of size n ^ n, but in this case, the space complexity will often explode, but we think of 1 ~ n The arrangement is only up to n! It is much smaller than n ^ n, so consider using a number to represent an arrangement and compress the space. Cantor expansion is to correspond a permutation to its ordinal number in the entire permutation, that is, the permutation of this permutation from small to large in all permutations. (Of course it can be arranged from large to small)

How to find an array of Cantor?

  There is a formula: R & lt A n- K = A . 1 ( n- - . 1 ) ! + A 2 ( n- - 2 ) ! + + A n- 0 , where the current rank is arranged x1, x2, ..., xn of! The Cantor expansion value, ai is behind xi, that is , the number of numbers less than xi in x i + 1 ~ x n .

(Explain digging)

1  int cantor ( int x [], int n)
 2 { // cantor expansion, n means a full arrangement of n bits, x [] means a fully arranged number (represented by an array) 
3      int ans = 0 , sum = 0 ;
 4      for ( int i = 1 ; i <n; i ++ ) {
 5          for ( int j = i + 1 ; j <= n; j ++ )
 6              if (x [j] < x [i])
 7                  sum ++; // Record how many 
8          ans + = sum * factorial [ni]; // the cumulative factorial is the preprocessed factorial value that is smaller than xi
9          sum = 0 ; // The counter is reset to 
10      }
 11      return ans + 1 ; // +1 is because the ordinal number is counted as itself. If you want to find how many permutations are in front of it, you do n’t need to add 
12 }

  example:

  Luogu P1379 Eight Digital Puzzle

  Typical use of Cantor expansion compression state

Expansion: Inverse Cantor expansion (digging)

Guess you like

Origin www.cnblogs.com/InductiveSorting-QYF/p/12679235.html