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)