# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
603036 | tutis | 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 "perm.h"
using namespace std;
#include <iostream>
#include <random>
#include <functional>
#include <map>
using ll = long long;
std::vector<int> construct_permutation(long long k, bool v)
{
if (k > 100)
{
if (k % 2 == 0)
{
vector<int>a = construct_permutation(k / 2, v);
a.push_back((int)a.size());
return a;
}
if (k % 4 == 3 && k > 1000)
{
vector<int>a = construct_permutation(k - 3, true);
for (int &i : a)
if (i >= 2)
i++;
a.push_back(2);
return a;
}
if (k % 2 == 1 && k > 1000)
{
vector<int>a = construct_permutation(k - 1, v);
for (int &i : a)
if (i >= 0)
i++;
a.push_back(0);
return a;
}
}
vector<int>ret = { -1};
vector<ll>dp = {1};
if (v)
{
assert(k >= 3);
vector<int>ret = { -1, 1, 0};
vector<ll>dp = {1, 1, 1};
}
for (int i : dp)
k -= i;
while (k != 0)
{
int sz = (int)ret.size();
for (int i = sz - 1; i >= 0; i--)
{
ll sum = 0;
for (int j = 0; j < (int)ret.size(); j++)
if (ret[j] < i)
sum += dp[j];
if (sum <= k)
{
k -= sum;
dp.push_back(sum);
for (int &j : ret)
if (j >= i)
j++;
ret.push_back(i);
break;
}
}
}
ret.erase(ret.begin());
return ret;
}
vector<int> construct_permutation(long long k)
{
return construct_permutation(k, false);
}