-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProblem_#24_Lexicographic_permutations.cpp
More file actions
69 lines (67 loc) · 1.43 KB
/
Problem_#24_Lexicographic_permutations.cpp
File metadata and controls
69 lines (67 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
int num;
vector<int> vis;
map<ll, int> want;
vector<string> ans;
string s = "abcdefghijklm";
void comp(string a){
if(a.length()==s.length()){
num++;
//cout << num << " "<<want[num].second << " " << want[num].first << " "<<a << endl;
if(want.find(num)!=want.end()){
ans[want[num]] = a;
}
return;
}
for(int i=0;i<s.length();i++){
if(!vis[i] ){
vis[i] = true;
comp(a+s[i]);
vis[i] = false;
}
}
}
vector<int> f(15, 1);
string solve(int n, string a, int fact){
if(a.length() == s.length()){
return a;
}
int ind = 0;
while(n > f[fact]){
n -= f[fact];
ind++;
}
for(int i=0;i<s.length();i++){
if(!vis[i]) ind--;
if(ind == -1){
vis[i] = true;
return solve(n, a+s[i], fact-1);
}
}
assert(1==0);
return "";
}
int main() {
int t;scanf("%d", &t);int T = t;
ans = vector<string>(T);
for(int i=1;i<15;i++){
f[i] = f[i-1]*i;
}
while(t--){
ll n; scanf("%lld", &n);
want[n] = T-t-1;
vis = vector<int>(13, 0);
printf("%s\n", solve(n, "", 12).c_str());
}
return 0;
}