QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#99890 | #6353. Kth Lex Min Min Min Subpalindromes | KKT89 | AC ✓ | 528ms | 10928kb | C++17 | 6.5kb | 2023-04-23 22:59:33 | 2023-04-23 22:59:36 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
inline double time() {
return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}
vector<vector<int>> r2 = {
{1,1,2,1,2,2},
{1,1,2,2,1,2},
{1,2,1,1,2,2},
{1,2,1,2,2,1},
{1,2,2,1,1,2},
{1,2,2,1,2,1},
{2,1,1,2,1,2},
{2,1,1,2,2,1},
{2,1,2,1,1,2},
{2,1,2,2,1,1},
{2,2,1,1,2,1},
};
// 0-indexed
template<typename T>
struct BIT{
int n;
vector<T> bit;
BIT (int n = 0) : n(n),bit(n+1) {}
// [0, i)
T sum (int i) {
T res = 0;
for (; i > 0; i -= (i&-i)) {
res += bit[i];
}
return res;
}
// [l, r)
T sum (int l, int r) {
return sum(r) - sum(l);
}
void add (int i, T a) {
i++;
for (; i <= n; i += (i&-i)) {
bit[i] += a;
}
}
int lower_bound (T k) { // k <= sum(res)
if (k <= 0) return 0;
int res = 0, i = 1;
while ((i << 1) <= n) i <<= 1;
for (; i ; i >>= 1) {
if (res+i <= n and bit[res+i] < k) {
k -= bit[res += i];
}
}
return res;
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
// if (true) {
// for (int i = 0; i < 12; ++i) {
// cout << "{";
// for (int j = 0; j < 6; ++j) {
// int a; cin >> a;
// if (j) cout << ",";
// cout << a+1;
// }
// cout << "}," << endl;
// }
// }
ll n,m,k; cin >> n >> m >> k;
if (m == 1) {
if (k == 1) {
for (int i = 0; i < n; ++i) {
if (i) cout << " ";
cout << 1;
}
cout << endl;
}
else {
cout << -1 << endl;
}
}
else if (m == 2) {
if (k > 12) {
cout << -1 << endl;
}
else if (n <= 6) {
vector<pair<int,vector<int>>>tmp;
for(int i = 0; i < (1 << n); i++) {
vector<int>a;
for(int j = 0; j < n; j++) {
a.push_back(1 & (i >> j));
}
int cnt = 0;
for(int j = 0; j < n; j++) {
for(int k = j; k < n; k++) {
vector<int>res;
for(int l = j; l <= k; l++) {
res.push_back(a[l]);
}
vector<int>rres = res;
reverse(rres.begin(),rres.end());
if(res == rres) {
cnt++;
}
}
}
tmp.push_back({cnt,a});
}
sort(tmp.begin(),tmp.end());
if (tmp.size() >= k and tmp[k-1].first == tmp[0].first) {
for (int i = 0; i < n; ++i) {
if (i) cout << " ";
cout << tmp[k-1].second[i]+1;
}
cout << endl;
}
else {
cout << -1 << endl;
}
}
else {
k--;
int j = 0;
for (int i = 0; i < n; ++i) {
if(i) cout << " ";
cout << r2[k][j];
j++; if (j == 6) j = 0;
}
cout << endl;
}
}
else if (n == 1) {
if (k <= m) {
cout << k << endl;
}
else {
cout << -1 << endl;
}
}
else {
// m*(m-1)*(m-2)*(m-2)…
bool ok = false;
ll mul = 1;
for (int i = 0; i < n; ++i) {
if (i == 0) {
mul *= m;
}
else if (i == 1) {
mul *= (m-1);
}
else {
// mul*(m-2) >= k
if (mul >= (k+m-3)/(m-2)) {
ok = true; break;
}
else {
mul *= (m-2);
}
}
}
if (mul >= k) ok = true;
if (!ok) {
cout << -1 << endl;
return 0;
}
if (m == 3) {
vector<int> p = {1,2,3};
int num = 0;
do {
num++;
if (num == k) {
for (int i = 0; i < n; ++i) {
if (i) cout << " ";
cout << p[i%3];
}
cout << endl;
}
} while (next_permutation(p.begin(), p.end()));
return 0;
}
else {
BIT<int> bit(m+1);
for (int i = 1; i <= m; ++i) {
bit.add(i, 1);
}
vector<int> res(n);
for (int i = 0; i < n; ++i) {
bool over = false;
ll r = 1;
// res[i] を決めたとする
{
for (int j = i+1; j < n; ++j) {
if (j == 1) {
r *= (m-1);
}
else {
if (r >= (k+m-3)/(m-2)) {
over = true; break;
}
else {
r *= (m-2);
}
}
}
}
if (over) {
res[i] = bit.lower_bound(1);
}
else {
ll u = (k-1)/r+1;
res[i] = bit.lower_bound(u);
k -= r*(u-1);
}
bit.add(res[i], -1);
if (i-2 >= 0) {
bit.add(res[i-2], 1);
}
// cout << res[i] << " " << r << " " << k << endl;
}
for (int i = 0; i < n; ++i) {
if (i) cout << " ";
cout << res[i];
}
cout << endl;
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3444kb
input:
1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3472kb
input:
2 2 2
output:
2 1
result:
ok 2 number(s): "2 1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3480kb
input:
3 3 3
output:
2 1 3
result:
ok 3 number(s): "2 1 3"
Test #4:
score: 0
Accepted
time: 2ms
memory: 3460kb
input:
9 9 8244353
output:
2 4 1 2 6 8 1 2 7
result:
ok 9 numbers
Test #5:
score: 0
Accepted
time: 2ms
memory: 3484kb
input:
10 7 998244353
output:
-1
result:
ok 1 number(s): "-1"
Test #6:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
3 1000 994253860
output:
998 244 353
result:
ok 3 number(s): "998 244 353"
Test #7:
score: 0
Accepted
time: 2ms
memory: 3380kb
input:
58 4 864691128455135232
output:
4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4 3 2 4
result:
ok 58 numbers
Test #8:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
58 4 864691128455135233
output:
-1
result:
ok 1 number(s): "-1"
Test #9:
score: 0
Accepted
time: 184ms
memory: 10928kb
input:
1000000 1000000 1000000000000000000
output:
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 ...
result:
ok 1000000 numbers
Test #10:
score: 0
Accepted
time: 528ms
memory: 7024kb
input:
1000000 4 1000000000000000000
output:
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 ...
result:
ok 1000000 numbers
Test #11:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
1 1 2
output:
-1
result:
ok 1 number(s): "-1"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3432kb
input:
1 2 2
output:
2
result:
ok 1 number(s): "2"
Test #13:
score: 0
Accepted
time: 2ms
memory: 3512kb
input:
2 2 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #14:
score: 0
Accepted
time: 2ms
memory: 3480kb
input:
3 2 4
output:
2 1 1
result:
ok 3 number(s): "2 1 1"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3476kb
input:
3 2 7
output:
-1
result:
ok 1 number(s): "-1"
Test #16:
score: 0
Accepted
time: 2ms
memory: 3524kb
input:
4 2 10
output:
2 2 1 2
result:
ok 4 number(s): "2 2 1 2"
Test #17:
score: 0
Accepted
time: 2ms
memory: 3440kb
input:
4 2 3
output:
1 2 1 1
result:
ok 4 number(s): "1 2 1 1"
Test #18:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
5 2 7
output:
2 1 1 2 1
result:
ok 5 number(s): "2 1 1 2 1"
Test #19:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
5 2 13
output:
-1
result:
ok 1 number(s): "-1"
Test #20:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
6 2 5
output:
1 2 2 1 1 2
result:
ok 6 numbers
Test #21:
score: 0
Accepted
time: 57ms
memory: 3524kb
input:
1000000 2 3
output:
1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 1 2 1 1 2 2 ...
result:
ok 1000000 numbers
Test #22:
score: 0
Accepted
time: 50ms
memory: 3508kb
input:
1000000 2 5
output:
1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 ...
result:
ok 1000000 numbers
Test #23:
score: 0
Accepted
time: 51ms
memory: 3512kb
input:
1000000 2 7
output:
2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 ...
result:
ok 1000000 numbers
Test #24:
score: 0
Accepted
time: 2ms
memory: 3476kb
input:
1000000 2 1000000000000000000
output:
-1
result:
ok 1 number(s): "-1"
Test #25:
score: 0
Accepted
time: 1ms
memory: 3432kb
input:
1 3 2
output:
2
result:
ok 1 number(s): "2"
Test #26:
score: 0
Accepted
time: 2ms
memory: 3480kb
input:
2 3 5
output:
3 1
result:
ok 2 number(s): "3 1"
Test #27:
score: 0
Accepted
time: 53ms
memory: 3452kb
input:
1000000 3 5
output:
3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 ...
result:
ok 1000000 numbers
Test #28:
score: 0
Accepted
time: 8ms
memory: 3408kb
input:
1000000 3 7
output:
-1
result:
ok 1 number(s): "-1"
Test #29:
score: 0
Accepted
time: 443ms
memory: 6968kb
input:
1000000 4 211106232532991
output:
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 ...
result:
ok 1000000 numbers
Test #30:
score: 0
Accepted
time: 366ms
memory: 6932kb
input:
1000000 5 1000000000000000000
output:
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 ...
result:
ok 1000000 numbers
Test #31:
score: 0
Accepted
time: 139ms
memory: 7400kb
input:
1000000 123123 1000000000000000000
output:
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 ...
result:
ok 1000000 numbers
Test #32:
score: 0
Accepted
time: 15ms
memory: 6880kb
input:
6 1000000 1000000000000000000
output:
1 2 4 9 15 8
result:
ok 6 numbers
Test #33:
score: 0
Accepted
time: 15ms
memory: 6952kb
input:
4 1000000 1000000000000000000
output:
2 7 15 9
result:
ok 4 number(s): "2 7 15 9"
Test #34:
score: 0
Accepted
time: 12ms
memory: 6908kb
input:
3 1000000 999997000002000000
output:
1000000 999999 999998
result:
ok 3 number(s): "1000000 999999 999998"
Test #35:
score: 0
Accepted
time: 0ms
memory: 3444kb
input:
3 1000000 999997000002000001
output:
-1
result:
ok 1 number(s): "-1"