QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#181181#6353. Kth Lex Min Min Min Subpalindromesucup-team859#WA 138ms12956kbC++175.9kb2023-09-16 16:20:542023-09-16 16:20:55

Judging History

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

  • [2023-09-16 16:20:55]
  • 评测
  • 测评结果:WA
  • 用时:138ms
  • 内存:12956kb
  • [2023-09-16 16:20:54]
  • 提交

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 += '2';
                    } else {
                        str += '1';
                    }
                }
                
                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: 0ms
memory: 3612kb

input:

1 1 1

output:

1 

result:

ok 1 number(s): "1"

Test #2:

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

input:

2 2 2

output:

2 1 

result:

ok 2 number(s): "2 1"

Test #3:

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

input:

3 3 3

output:

2 1 3 

result:

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

Test #4:

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

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

input:

10 7 998244353

output:

-1

result:

ok 1 number(s): "-1"

Test #6:

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

input:

3 1000 994253860

output:

998 244 353 

result:

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

Test #7:

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

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: 0ms
memory: 3540kb

input:

58 4 864691128455135233

output:

-1

result:

ok 1 number(s): "-1"

Test #9:

score: 0
Accepted
time: 122ms
memory: 11224kb

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: 138ms
memory: 11088kb

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: 0ms
memory: 3544kb

input:

1 1 2

output:

-1

result:

ok 1 number(s): "-1"

Test #12:

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

input:

1 2 2

output:

2 

result:

ok 1 number(s): "2"

Test #13:

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

input:

2 2 1

output:

1 2 

result:

ok 2 number(s): "1 2"

Test #14:

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

input:

3 2 4

output:

2 1 1 

result:

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

Test #15:

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

input:

3 2 7

output:

-1

result:

ok 1 number(s): "-1"

Test #16:

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

input:

4 2 10

output:

2 2 1 2 

result:

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

Test #17:

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

input:

4 2 3

output:

1 2 1 1 

result:

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

Test #18:

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

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: 0ms
memory: 3820kb

input:

5 2 13

output:

-1

result:

ok 1 number(s): "-1"

Test #20:

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

input:

6 2 5

output:

1 2 2 1 1 2 

result:

ok 6 numbers

Test #21:

score: 0
Accepted
time: 55ms
memory: 12940kb

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

input:

1000000 2 5

output:

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 1 ...

result:

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