NYOJ143 Who is the first? (inverse Cantor expansion)

describe

Now there are 12 characters of "abcdefghijkl", put them in lexicographical order, if any permutation is given, we can say which permutation is the smallest among all permutations. But now we give it what is the smallest, and you need to find the sequence it represents.

enter

The first line has an integer n (0

output

Output a sequence, occupying one line, representing the mth smallest sequence.

sample input

3
1
302715242
260726926

Sample output

abcdefghijkl
hgebkflacdji
gfkedhjblcia

ideas

nyoj139The mirror title of , uses the inverse expansion of Kang Tuo, about Kang Tuo, you can check Wikipedia:Can Tuo expansion

code

#include <bits/stdc++.h>
using namespace std;
int jc[13]= {1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600};
int num[13];//num[i]存储从小到大第i个未用过的字母编号
void init()
{
    for(int i=0; i<13; i++)
        num[i]=i;
}
string get_string(int cnt)//逆展开
{
    string ans="";
    for(int i=0; i<12; i++)
    {
        int tmp=cnt/jc[11-i];
        ans+=num[tmp]+'a';
        for(int j=tmp; j<11; j++)
            num[j]=num[j+1];
        cnt%=jc[11-i];
    }
    return ans;
}
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        init();
        cout<<get_string(n-1)<<endl;
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324584008&siteId=291194637