QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#240533 | #6353. Kth Lex Min Min Min Subpalindromes | jzh# | WA | 0ms | 3832kb | C++20 | 5.5kb | 2023-11-05 16:13:12 | 2023-11-05 16:13:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
constexpr ll mx = ULONG_LONG_MAX;
ll qpow(ll a, ll b) {
ll ans = 1;
while(b) {
if(b&1) {
if(ans>mx/a) return mx;
else ans *= a;
}
if(a>mx/a) return mx;
else a *= a;
b/=2;
}
return ans;
}
ll mul(ll a, ll b) {
if(a>mx/b) return mx;
return a*b;
}
struct info{
vector<int>vec;
int last;
explicit info(vector<int>temp) {
vec = temp;
for(int i = 2 ; i < temp.size() ; i ++) {
bool flag = true;
for(int j = temp.size() - i, k = temp.size()-1 ; j < k ; j ++, k--){
if(temp[j]!=temp[k]) flag = false;
}
if(flag) {
last = i;
break;
}
}
}
void advance(){
int cnt0 = 0, cnt1 = 0;
if(vec.back()==0) cnt0++;
else cnt1++;
if(*prev(end(vec), 2)==0) cnt0++;
else cnt1++;
if(*prev(end(vec), last+1)==0) cnt0++;
else cnt1++;
if(cnt0<cnt1) {
if(vec.back()==0) {
last = 2;
}
else if(*prev(end(vec), 2)==0) {
last = 3;
}
else{
last += 2;
}
vec.push_back(0);
}
else{
if(vec.back()==1) {
last = 2;
}
else if(*prev(end(vec), 2)==1) {
last = 3;
}
else{
last += 2;
}
vec.push_back(1);
}
}
};
void solve() {
ll n, m, k; cin>>n >>m >>k;
if(n==1) {
if(k<=m) cout << k << endl;
else cout << -1 << endl;
return;
}
if(n==2) {
if(m *(m-1) >= k) {
k--;
ll a = k / (m-1) + 1;
ll b = k % (m-1) + 1;
if(b>=a) b ++;
cout << a << ' ' << b << endl;
}
else{
cout << -1 << endl;
}
return;
}
if(m==1){
if(k==1) {
for(int i = 0 ; i < n ; i ++) cout << 1 << ' '; cout << endl;
return;
}
else{
cout << "-1" << endl;
return;
}
}
else if(m==2) {
if(n<=15) {
set<vector<int>>all;
auto calc = [&](vector<int>vec) {
int ans = 0;
for(int i = 0 ; i < n ; i ++) {
for(int j = i ; j < n ; j ++){
bool flag = true;
for(int k = i ; i+j-k > k ; k ++) {
if(vec[k]!=vec[i+j-k]) flag = false;
}
ans += flag;
}
}
return ans;
};
vector<int>temp(n);
for(int st = 0 ; st < (1<<n) ; st ++) {
for(int i = 0 ; i < n ; i ++) {
temp[i] = (st>>i&1);
}
if(calc(temp)==2*n-2) all.insert(temp);
}
if(all.size()<k) {
cout << "-1\n";
return;
}
auto vec = *next(begin(all), k-1);
for(auto &v: vec) cout << v << ' '; cout << endl;
}
else{
if(k>12) {
cout << "-1" << endl;
return;
}
int len = 10;
vector<info>all;
auto calc = [&](vector<int>vec) {
int ans = 0;
for(int i = 0 ; i < len ; i ++) {
for(int j = i ; j < len ; j ++){
bool flag = true;
for(int k = i ; i+j-k > k ; k ++) {
if(vec[k]!=vec[i+j-k]) flag = false;
}
ans += flag;
}
}
return ans;
};
vector<int>temp(len);
for(int st = 0 ; st < (1<<len) ; st ++) {
for(int i = 0 ; i < len ; i ++) {
temp[i] = (st>>i&1);
}
if(calc(temp)==2*len-2) all.emplace_back(info(temp));
}
for(int i = len ; i < n ; i ++) {
for(auto &it: all) it.advance();
}
set<vector<int>>st;
for(auto &it: all) st.insert(it.vec);
auto vec = *next(begin(st), k-1);
for(auto &v: vec) cout << v <<' '; cout<< endl;
return;
}
}
else{
if( mul(mul(m, (m-1)),qpow(m-2, n-2)) < k) {
cout << "-1\n";
return;
}
k--;
vector<ll>ans(n);
ll sz2 = qpow(m-2, n-2), sz1 = sz2 * (m-1);
ans[0] = k / sz1 + 1;
k -= (k/sz1) * sz1;
ans[1] = k / sz2 + 1;
k -= (k/sz2) *sz2;
if(ans[1]>=ans[0]) ans[1] ++;
for(int i = 2 ; i < n ; i ++){
sz2 = qpow(m-2, n-i-1);
ans[i] = k / sz2 + 1;
if(ans[i]>=min(ans[i-1], ans[i-2])) ans[i]++;
if(ans[i]>=max(ans[i-1], ans[i-2])) ans[i]++;
k -= (k/sz2) *sz2;
}
for(int i = 0 ; i < n ; i ++)cout <<ans[i] << ' '; cout <<endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
input:
1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
2 2 2
output:
2 1
result:
ok 2 number(s): "2 1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
3 3 3
output:
2 1 3
result:
ok 3 number(s): "2 1 3"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
9 9 8244353
output:
2 4 1 2 6 8 1 2 7
result:
ok 9 numbers
Test #5:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
10 7 998244353
output:
-1
result:
ok 1 number(s): "-1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
3 1000 994253860
output:
998 244 353
result:
ok 3 number(s): "998 244 353"
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 3756kb
input:
58 4 864691128455135232
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 402653186 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
result:
wrong answer 1st numbers differ - expected: '4', found: '1'