LightOJ1060 nth permutation (non-repeating full arrangement + reverse Cantor expansion)

Source: Internet
Author: User

More than a year ago met the topic http://acm.fafu.edu.cn/problem.php?id=1427.

At first I also used a search. Then I accidentally found a calculated formula that does not repeat the full permutation: m!/(n1!*n2!*...*nn!),

Then on their own yy out of the solution, made a few days, finally to the seniors to the data, and then vaguely adjusted, finally AC.

Later only to know that the solution was similar to the inverse Cantor deployment, but the inverse of the Cantor expansion is not repeated elements of the whole arrangement, but there are no duplicate elements are the same.

And now do this very well, because the idea is very clear, in addition, this method and the number of number theory DP statistical parts have similarities.

1#include <cstdio>2#include <cstring>3 using namespacestd;4 Long Longfact[ +]={1};5 Long LongCalU (intNint*CNT) {6     Long Longres=Fact[n];7      for(intI=0; i< -; ++i) res/=Fact[cnt[i]];8     returnRes;9 }Ten intMain () { One      for(intI=1; i< +; ++i) fact[i]=fact[i-1]*i; A     Charstr[ A]; -     Long LongN; -     intT; thescanf"%d",&t); -      for(intCse=1; cse<=t; ++CSE) { -scanf"%s%lld",str,&n); -         intSn=strlen (str), cnt[ -]={0}; +          for(intI=0; i<sn; ++i) ++cnt[str[i]-'a']; -         if(CalU (SN,CNT) <N) { +printf"Case %d:impossible\n", CSE); A             Continue; at         } -printf"Case %d:", CSE); -          for(intI=0; i<sn; ++i) { -              for(intj=0; j< -; ++j) { -                 if(cnt[j]==0)Continue; ---Cnt[j]; in                 if(N>calu (sn-i-1, CNT)) { -N-=calu (sn-i-1, CNT); to++Cnt[j]; +}Else{ -Putchar (j+'a'); the                      Break; *                 } $             }Panax Notoginseng         } -Putchar ('\ n'); the     } +     return 0; A}

LightOJ1060 nth permutation (non-repeating full arrangement + reverse Cantor expansion)

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.