#include <iostream>
#include <string>
using namespace std;
int GetFactorial(int n)
{
int r = n;
while(--n>0)r *= n;
return r;
}
string Permutation(int n, int kth)
{
string num('0',n);
int i = -1;
while(++i < n) num[i] = i + '1';
int fact = GetFactorial(n-1);
int index = 0;
string s;
kth = kth - 1;
while(--n > 0)
{
index = kth / fact;
s.push_back(num[index]);
num.erase(index, 1);
kth = kth % fact;
fact = fact / n;
}
s.push_back(num[0]);
return s;
}
int main(int argc, char** argv)
{
cout << Permutation(3, 5) << '\n';
cout << Permutation(9, GetFactorial(7)) << '\n';
return 0;
}