- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem024.cpp
More file actions
Latest commit
32 lines (30 loc) · 731 Bytes
/
Copy pathproblem024.cpp
File metadata and controls
32 lines (30 loc) · 731 Bytes
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
#include<iostream>
#include<vector>
std::vector<int> nthPermutation(int n, int elements)
{
std::vector<int> a(elements);
int x = n;
// Converts n to factoradic to obtain a Lehmer code.
for (int i = 2; i <= elements; ++i) {
a[elements - i] = x % i;
x /= i;
}
// Decodes the Lehmer code into a permutation.
for (int i = elements - 2; i >= 0; --i) {
for (int j = i + 1; j < elements; ++j) {
if (a[j] >= a[i]) {
++a[j];
}
}
}
return a;
}
intmain()
{
std::vector<int> ans = nthPermutation(999999, 10);
for (int i = 0; i < ans.size(); ++i) {
std::cout << ans[i];
}
std::cout << '\n';
return0;
}