Submission #812530

# Submission time Handle Problem Language Result Execution time Memory
812530 2023-08-07T09:07:00 Z piOOE Ancient Machine 2 (JOI23_ancient2) C++17
100 / 100
87 ms 764 KB
#include "ancient2.h"
#include <bits/stdc++.h>

using namespace std;

namespace {
    constexpr int maxN = 1001;
    int n, Q = 0, basisSize = 0;

    bool findPrefixBit(int m) { // 0 indexed
        vector<int> a(m + 3), b(m + 3);
        for (int i = 0; i < m; ++i) {
            a[i] = b[i] = i + 1;
        }
        a[m + 1] = b[m + 1] = m + 1;
        a[m + 2] = b[m + 2] = m + 2;
        a[m] = m + 1, b[m] = m + 2;
        Q += 1;
        return Query(size(a), a, b) == m + 2;
    }

    int findLongestPrefix(const string &s, const string &t) {
        for (int len = min(size(s), size(t)); len > 0; --len) {
            if (s.substr(0, len) == t.substr(size(t) - len)) {
                return len;
            }
        }
        return 0;
    }

    bool findSuffixBit(int m, string suf) { // find m-th last bit (1 indexed, i.e. the last char is the 1st)
        suf = '1' + suf;
        vector<int> a(m + 1), b(m + 1);
        a[m] = b[m] = m;
        for (int i = 0; i <= m; ++i) {
            string sa = suf.substr(0, i) + '0';
            string sb = suf.substr(0, i) + '1';
            a[i] = findLongestPrefix(suf, sa);
            b[i] = findLongestPrefix(suf, sb);
        }
        Q += 1;
        return Query(m + 1, a, b) == m;
    }

    bitset<maxN> basis[maxN];

    int query(bitset<maxN> a) {
        for (int i = 0; i < n; ++i) {
            if (!a[i]) {
                continue;
            }
            if (basis[i].none()) {
                return -1;
            }
            a ^= basis[i];
        }
        return a[n];
    }

    bool insert(bitset<maxN> a) {
        for (int i = 0; i < n; ++i) {
            if (!a[i]) {
                continue;
            }
            if (basis[i].none()) {
                basis[i] = a;
                basisSize += 1;
                return true;
            }
            a ^= basis[i];
        }
        return false;
    }

    int findXor(int p, int q) { // finds xor of s[x] (x = q + p * k)
        int m = p * 2;
        vector<int> a(m);
        iota(a.begin(), a.end(), 1), iota(a.begin() + p, a.end(), p + 1);
        a[p - 1] = 0, a[m - 1] = p;
        vector<int> b = a;
        b[q] += p, b[q + p] -= p;
        Q += 1;
        return Query(m, a, b) >= p;
    }
}  // namespace

std::string Solve(int N) {
    n = N;
    constexpr int M = 100;
    vector<int> ans(n, -1);

    for (int i = 0; i < min(n, M); ++i) {
        ans[i] = findPrefixBit(i);
        bitset<maxN> b{};
        b[i] = 1, b[n] = ans[i];
        insert(b);
    }

    string suf;
    for (int i = n - 1; i >= max(n - M, M); --i) {
        ans[i] = findSuffixBit(n - i, suf);
        suf = char('0' + ans[i]) + suf;
        bitset<maxN> b{};
        b[i] = 1, b[n] = ans[i];
        insert(b);
    }

    mt19937 rnd(228);

    while (Q < 1000 && basisSize < n) {
        constexpr int R = 51;
        int p = rnd() % R + 1, q = rnd() % p;
        bitset<maxN> b{};
        for (int i = q; i < n; i += p) {
            b[i] = 1;
        }
        if (query(b) == -1) {
            b[n] = findXor(p, q);
            insert(b);
        }
    }

    for (int i = M; i + M < n; ++i) {
        bitset<maxN> b{};
        b[i] = 1;
        ans[i] = query(b);
    }

    string s(n, '?');
    for (int i = 0; i < n; ++i) {
        s[i] = '0' + ans[i];
    }
    return s;
}
# Verdict Execution time Memory Grader output
1 Correct 73 ms 428 KB Output is correct
2 Correct 61 ms 336 KB Output is correct
3 Correct 63 ms 432 KB Output is correct
4 Correct 63 ms 412 KB Output is correct
5 Correct 64 ms 412 KB Output is correct
6 Correct 61 ms 372 KB Output is correct
7 Correct 65 ms 488 KB Output is correct
8 Correct 65 ms 416 KB Output is correct
9 Correct 65 ms 420 KB Output is correct
10 Correct 65 ms 420 KB Output is correct
11 Correct 67 ms 364 KB Output is correct
12 Correct 63 ms 360 KB Output is correct
13 Correct 64 ms 472 KB Output is correct
14 Correct 64 ms 432 KB Output is correct
15 Correct 64 ms 400 KB Output is correct
16 Correct 66 ms 432 KB Output is correct
17 Correct 61 ms 432 KB Output is correct
18 Correct 70 ms 764 KB Output is correct
19 Correct 63 ms 428 KB Output is correct
20 Correct 61 ms 336 KB Output is correct
21 Correct 63 ms 352 KB Output is correct
22 Correct 61 ms 336 KB Output is correct
23 Correct 62 ms 508 KB Output is correct
24 Correct 61 ms 436 KB Output is correct
25 Correct 63 ms 464 KB Output is correct
26 Correct 65 ms 372 KB Output is correct
27 Correct 61 ms 348 KB Output is correct
28 Correct 64 ms 400 KB Output is correct
29 Correct 61 ms 420 KB Output is correct
30 Correct 61 ms 432 KB Output is correct
31 Correct 72 ms 348 KB Output is correct
32 Correct 70 ms 368 KB Output is correct
33 Correct 60 ms 428 KB Output is correct
34 Correct 72 ms 348 KB Output is correct
35 Correct 65 ms 340 KB Output is correct
36 Correct 61 ms 348 KB Output is correct
37 Correct 63 ms 428 KB Output is correct
38 Correct 74 ms 344 KB Output is correct
39 Correct 64 ms 348 KB Output is correct
40 Correct 63 ms 412 KB Output is correct
41 Correct 69 ms 368 KB Output is correct
42 Correct 64 ms 432 KB Output is correct
43 Correct 60 ms 356 KB Output is correct
44 Correct 71 ms 428 KB Output is correct
45 Correct 63 ms 400 KB Output is correct
46 Correct 72 ms 424 KB Output is correct
47 Correct 61 ms 352 KB Output is correct
48 Correct 84 ms 428 KB Output is correct
49 Correct 64 ms 420 KB Output is correct
50 Correct 87 ms 428 KB Output is correct
51 Correct 61 ms 428 KB Output is correct
52 Correct 71 ms 532 KB Output is correct
53 Correct 72 ms 396 KB Output is correct
54 Correct 68 ms 428 KB Output is correct
55 Correct 73 ms 348 KB Output is correct
56 Correct 63 ms 428 KB Output is correct
57 Correct 74 ms 432 KB Output is correct
58 Correct 70 ms 432 KB Output is correct
59 Correct 67 ms 428 KB Output is correct
60 Correct 64 ms 428 KB Output is correct
61 Correct 74 ms 356 KB Output is correct
62 Correct 80 ms 428 KB Output is correct
63 Correct 64 ms 432 KB Output is correct