#include <bits/stdc++.h>
#include "perm.h"
using namespace std;
typedef long long ll;
vector<int> dp = {-1, 0, 1};
vector<vector<int>> dpConst = {{}, {}, {0}};
int calcMin(ll id, ll k)
{
if (id >= k) return dp[k];
vector<int> digs;
while (k > 0) digs.push_back(k % id), k /= id;
reverse(digs.begin(), digs.end());
int ans = (digs.size()-1)*dp[id];
ans += dp[digs[0]];
for (int i = 1; i < digs.size(); i++) ans += dp[digs[i]+1];
return ans;
}
vector<int> constMin(ll id, ll k)
{
if (id >= k) return dpConst[k];
vector<int> digs;
while (k > 0) digs.push_back(k % id), k /= id;
reverse(digs.begin(), digs.end());
vector<int> ans = dpConst[digs[0]];
for (int i = 1; i < digs.size(); i++)
{
vector<int> aux = dpConst[id];
for (auto x : ans) aux.push_back(x+dp[id]);
swap(aux, ans); aux.clear();
for (auto x : dpConst[digs[i]+1]) aux.push_back(x + ans.size());
for (auto x : ans) aux.push_back(x);
swap(ans, aux);
}
return ans;
}
void init()
{
if (dp.size() == 5000) return;
for (int k = 3; k <= 5000; k++)
{
int ans = 120, ansId = 2;
for (int id = 2; id < dp.size(); id++)
{
int aux = calcMin(id, k);
if (aux < ans) ans = aux, ansId = id;
}
dp.push_back(ans);
vector<int> aux = constMin(ansId, k);
dpConst.push_back(aux);
}
}
vector<int> construct_permutation(long long k)
{
int resp = 120, id = 2;
for (int i = 2; i < dp.size(); i++)
{
int aux = calcMin(i, k);
if (aux < resp) resp = aux, id = i;
}
return constMin(id, k);
}
Compilation message
perm.cpp: In function 'int calcMin(ll, ll)':
perm.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i = 1; i < digs.size(); i++) ans += dp[digs[i]+1];
| ~~^~~~~~~~~~~~~
perm.cpp: In function 'std::vector<int> constMin(ll, ll)':
perm.cpp:27:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 1; i < digs.size(); i++)
| ~~^~~~~~~~~~~~~
perm.cpp: In function 'void init()':
perm.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int id = 2; id < dp.size(); id++)
| ~~~^~~~~~~~~~~
perm.cpp: In function 'std::vector<int> construct_permutation(long long int)':
perm.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 2; i < dp.size(); i++)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Partially correct |
2 ms |
336 KB |
Partially correct |
6 |
Correct |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
3 ms |
336 KB |
Output is correct |
8 |
Partially correct |
3 ms |
336 KB |
Partially correct |
9 |
Correct |
3 ms |
336 KB |
Output is correct |
10 |
Partially correct |
3 ms |
336 KB |
Partially correct |
11 |
Partially correct |
3 ms |
336 KB |
Partially correct |
12 |
Partially correct |
2 ms |
336 KB |
Partially correct |
13 |
Partially correct |
3 ms |
336 KB |
Partially correct |