QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99891#6353. Kth Lex Min Min Min SubpalindromesKKT89WA 2ms3512kbC++176.5kb2023-04-23 23:00:032023-04-23 23:00:06

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-23 23:00:06]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3512kb
  • [2023-04-23 23:00:03]
  • 提交

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-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-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;
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3376kb

input:

1 1 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3376kb

input:

2 2 2

output:

2 1

result:

ok 2 number(s): "2 1"

Test #3:

score: 0
Accepted
time: 2ms
memory: 3504kb

input:

3 3 3

output:

2 1 3

result:

ok 3 number(s): "2 1 3"

Test #4:

score: 0
Accepted
time: 2ms
memory: 3412kb

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: 3428kb

input:

10 7 998244353

output:

-1

result:

ok 1 number(s): "-1"

Test #6:

score: 0
Accepted
time: 2ms
memory: 3512kb

input:

3 1000 994253860

output:

998 244 353

result:

ok 3 number(s): "998 244 353"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3480kb

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: -100
Wrong Answer
time: 2ms
memory: 3444kb

input:

58 4 864691128455135233

output:

5 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:

wrong answer 1st numbers differ - expected: '-1', found: '5'