QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#181178 | #6353. Kth Lex Min Min Min Subpalindromes | ucup-team859# | WA | 1ms | 3448kb | C++17 | 5.9kb | 2023-09-16 16:19:44 | 2023-09-16 16:19:45 |
Judging History
answer
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
using ull = unsigned long long;
using ll = long long;
using namespace std;
constexpr ll INF = 2e18;
inline ll mul(ll a, ll b) {
if (INF / b <= a) {
return INF;
}
return a * b;
}
inline ll logpow(ll a, ll b) {
ll result = 1;
while (b > 0) {
if (b & 1) result = mul(a, result);
b >>= 1;
a = mul(a, a);
}
return result;
}
int main() {
#ifdef HOME
ifstream cin("input.in");
ofstream cout("output.out");
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m;
ll k;
cin >> n >> m >> k;
if (m == 1) {
if (k > 1) {
cout << -1;
return 0;
}
for (int i = 1; i <= n; i++) {
cout << 1 << " ";
}
return 0;
}
if (m == 2) {
if (n <= 10) {
vector<string> strings;
int result = 1e9;
auto Get = [](string str) {
int n = (int)str.size();
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int l = i, r = j;
while (l < r && str[l] == str[r]) {
l++, r--;
}
if (l >= r) {
cnt++;
}
}
}
return cnt;
};
for (int mask = 0; mask < (1 << n); mask++) {
string str;
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
str += '1';
} else {
str += '0';
}
}
int cnt = Get(str);
if (result > cnt) {
strings.clear();
result = cnt;
}
if (result == cnt) {
strings.push_back(str);
}
}
sort(strings.begin(), strings.end());
strings.resize(unique(strings.begin(), strings.end()) - strings.begin());
if ((int)strings.size() < k) {
cout << -1;
} else {
for (auto itr : strings[k - 1]) {
cout << itr << " ";
}
}
return 0;
}
vector<string> strings;
int result = (int) 1e9;
auto Get = [](string str) {
int n = (int)str.size();
int cnt = n;
for (int i = 0; i < n; i++) {
if (i + 1 < n && str[i] == str[i + 1]) {
cnt++;
}
if (i + 2 < n && str[i] == str[i + 2]) {
cnt++;
}
if (i + 3 < n && str[i] == str[i + 3] && str[i + 1] == str[i + 2]) {
cnt++;
}
}
return cnt;
};
for (char first = '1'; first <= '2'; first++) {
char second = '1' - first + '2';
for (int x = 1; x <= 2; x++) {
for (int y = 1; y <= 2; y++) {
int pos = 0;
string str;
auto Add = [n](string& str, char ch, int len) {
while ((int)str.size() < n && len > 0) {
len--;
str += ch;
}
};
while ((int)str.size() < n) {
if (pos % 4 == 0) {
Add(str, first, x);
} else if (pos % 4 == 1) {
Add(str, second, y);
} else if (pos % 4 == 2) {
Add(str, first, 3 - x);
} else {
Add(str, second, 3 - y);
}
pos++;
}
int cnt = Get(str);
if (cnt < result) {
result = cnt;
strings.clear();
}
if (cnt == result) {
strings.push_back(str);
}
}
}
}
sort(strings.begin(), strings.end());
strings.resize(unique(strings.begin(), strings.end()) - strings.begin());
// for (auto itr : strings) {
// cerr << itr << " " << Get(itr) << "\n";
// }
if ((int)strings.size() < k) {
cout << -1;
} else {
for (auto itr : strings[k - 1]) {
cout << itr << " ";
}
}
return 0;
}
ll total = mul(m, mul(m - 1, logpow(m - 2, n - 2)));
if (total < k) {
cout << -1;
return 0;
}
vector<ll> v(n);
ll coef0 = mul(m - 1, logpow(m - 2, n - 2));
ll v0 = (k - 1) / coef0 + 1;
k -= (v0 - 1) * coef0;
v[0] = v0;
ll coef1 = logpow(m - 2, n - 2);
ll v1 = (k - 1) / coef1 + 1;
k -= (v1 - 1) * coef1;
v[1] = (v1 < v0 ? v1 : v1 + 1);
for (int i = 2; i < n; i++) {
ll coef = logpow(m - 2, n - i - 1);
ll vi = (k - 1) / coef + 1;
k -= (vi - 1) * coef;
if (vi >= min(v[i - 1], v[i - 2])) {
vi++;
}
if (vi >= max(v[i - 1], v[i - 2])) {
vi++;
}
v[i] = vi;
}
for (auto itr : v) {
cout << itr << " ";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3448kb
input:
1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3448kb
input:
2 2 2
output:
1 0
result:
wrong answer 1st numbers differ - expected: '2', found: '1'