Cantor Expansion/counter-Cantor unfold

Source: Internet
Author: User

Move it first. Wikipedia's Cantor unfold (poke):

Cantor unfold is a double shot that is fully aligned to a natural number, often used to compress the space when building a hash table. The essence of Cantor's unfolding is that it calculates the order of the current arrangement in all the small-to-daquan permutations, and therefore is reversible.

Because it is double-shot, it is possible to find the full array of n in the K-Large arrangement (the inverse Cantor expands)

(pseudo) Calculation principle: From an element to find the number of smaller than this element, and then multiplied by the position of each number can have the number of combinations (permutation/factorial), to consider only from the beginning to the end of this bit than the current small number of permutations, and then add up is the number of Cantor expansion (pursuit of difficult to understand the peak ........... If you don't understand, just look at the wiki ...

Formula:

Ai is an integer and 0 <= ai < i, 1 <= i <= n

The meaning of AI: Ponder Chestnut bar

Chestnuts:

For example3 5 7 4 1 2 9 6 8Expand to98884. Because X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884. Explanation: The first digit of the permutation is 3, which has two of the number smaller than 3, and the arrangement with such a number begins with 8, so the first one is the8!the second digit of the arrangement is 5, which is 5 smaller than the number 1,2、3、4, since 3 has appeared, so there are 3 smaller than 5 of the number, so the arrangement has 7!, so the second one is the7!and so on until 0*0!

Put it on again (poke) Nocow about Cantor unfold page (stamp), there is code, can understand almost

The counter-Cantor unfolding is not a cantor unfolding completely upside down:

1. Subtract 1 to get the number of permutations smaller than the permutation

2. Starting from the High: take the factorial of the corresponding bit, get the number behind this bit smaller than this bit (so pay attention to the front than this bit of the number of small to remove)

    

Chestnuts:

such as n=5, x=96 o'clock:    first with1 to get 95, stating that x before there are 95 permutations. ( Subtract this number itself! )   Remove 4 with 95!    get 3 More than 23, indicating that there are 3 numbers smaller than the 1th bit, so the first bit is 4.    Remove 3 with 23!     get 3 More than 5, indicating that there are 3 numbers smaller than the 2nd bit, so is 4, but 4 has appeared    , therefore 5.   Remove 2 with 5!     get 2 more than 1, similarly, this bit is 3.   Remove 1 with 1!     get 1 more than 0, this bit is 2.   The last one can only be 1.   So this number is 45321.

  

Again, forget when to write the code:

 

1typedefLong Longll;2 Constll factorial[ One] = {1,1,2,6, -, -,720,5040,40320,362880,3628800};3 intnum[101];4ll Cantorex (intN) {5      for(inti =0; I < n; ++i) Cin >>Num[i];6ll sum =0;7      for(inti =0; I < n; ++i) {8         intt =0;9          for(intj = i +1; J < N; ++J)if(Num[j] < Num[i]) + +T;Tensum + = T * factorial[n-i-1]; One     } A     returnsum; - } -ll Ucantorex (ll Sum,intN) { thememset (NUM,0,sizeof(num)); -Sum-=1; -     intx =0; -ll ans =0; +      for(inti = n-1; i >0; --i) { -ll k = Sum/factorial[i] +1; +Sum%=Factorial[i]; A          while(Num[k]) + +K; atNUM[K] =1; -Ans = ans *Ten+K; -     } -      for(inti =1; I <= N; ++i) -         if(!num[i]) ans = ans *Ten+i; -     returnans; in}

done!

Cantor Expansion/counter-Cantor unfold

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.