#include "perm.h"
using namespace std;
#include <iostream>
#include <random>
#include <functional>
using ll = long long;
std::vector<int> construct_permutation(long long k)
{
vector<int>ret = { -1};
vector<ll>dp = {1};
k--;
while (k != 0)
{
vector<int>ret_ = ret;
vector<ll>dp_ = dp;
ll k_ = k;
function<void(int)>dfs = [&](int l)
{
if (k < k_)
{
k_ = k;
dp_ = dp;
ret_ = ret;
}
if (l == 0)
return;
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);
dfs(l - 1);
k += sum;
dp.pop_back();
ret.pop_back();
}
}
};
dfs(3);
ret = ret_;
dp = dp_;
k = k_;
}
ret.erase(ret.begin());
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
51 ms |
304 KB |
Output is correct |
4 |
Incorrect |
419 ms |
340 KB |
Integer 58 violates the range [0, 56] |
5 |
Halted |
0 ms |
0 KB |
- |