# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
737372 | yellowtoad | Permutation (APIO22_perm) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
int[] construct_permutation(int64 k) {
long long int n = k, cnt = 1, sz, a[110];
for (int i = 1; i <= 100; i++) a[i] = 0;
while ((1LL<<cnt)-1 <= n) cnt++;
cnt--;
sz = cnt;
n -= (1LL<<cnt)-1;
for (int i = 1; i <= cnt; i++) a[i] = i-1;
while (cnt >= 0) {
if ((1LL<<cnt) <= n) {
n -= (1LL<<cnt);
for (int i = sz; i >= 1; i--) a[i+1] = a[i];
sz++;
if (cnt) a[1] = a[sz-cnt+1];
else a[1] = a[sz]+1;
for (int i = sz; i >= sz-cnt+1; i--) a[i]++;
}
cnt--;
}
long long ans[sz];
for (int i = 0; i < sz; i++) ans[i] = a[i+1];
return ans[i];
}
/*int main() {
long long int k, b[];
cin >> k;
b = construct_permutation(int64 k);
for (int i = 0; i < sz; i++) cout << b[i] << " ";
}*/