답안 #812522

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
812522 2023-08-07T09:05:01 Z piOOE Ancient Machine 2 (JOI23_ancient2) C++17
100 / 100
96 ms 608 KB
#include "ancient2.h"
#include <bits/stdc++.h>

using namespace std;

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

    void check(const vector<int> &a) {
        assert(*max_element(a.begin(), a.end()) < size(a));
        assert(*min_element(a.begin(), a.end()) >= 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;
//        check(a), check(b);
        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;
//        check(a);
//        check(b);
        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;
//        check(a);
//        check(b);
        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) {
        int init = basisSize;
        while (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);
                assert(insert(b));
                break;
            }
        }
        assert(basisSize != init);
    }

    assert(basisSize == n);

    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) {
        if (ans[i] == -1) {
            while (true) {
                cout << "wtf" << endl;
            }
        }
        assert(ans[i] != -1);
        s[i] = '0' + ans[i];
    }
    return s;
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from ancient2.cpp:2:
ancient2.cpp: In function 'void {anonymous}::check(const std::vector<int>&)':
ancient2.cpp:11:49: warning: comparison of integer expressions of different signedness: 'const int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |         assert(*max_element(a.begin(), a.end()) < size(a));
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
ancient2.cpp: At global scope:
ancient2.cpp:10:10: warning: 'void {anonymous}::check(const std::vector<int>&)' defined but not used [-Wunused-function]
   10 |     void check(const vector<int> &a) {
      |          ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 432 KB Output is correct
2 Correct 69 ms 428 KB Output is correct
3 Correct 65 ms 336 KB Output is correct
4 Correct 68 ms 596 KB Output is correct
5 Correct 67 ms 420 KB Output is correct
6 Correct 69 ms 432 KB Output is correct
7 Correct 70 ms 336 KB Output is correct
8 Correct 83 ms 412 KB Output is correct
9 Correct 67 ms 608 KB Output is correct
10 Correct 68 ms 544 KB Output is correct
11 Correct 69 ms 432 KB Output is correct
12 Correct 66 ms 424 KB Output is correct
13 Correct 69 ms 336 KB Output is correct
14 Correct 72 ms 496 KB Output is correct
15 Correct 66 ms 408 KB Output is correct
16 Correct 82 ms 428 KB Output is correct
17 Correct 67 ms 544 KB Output is correct
18 Correct 66 ms 432 KB Output is correct
19 Correct 68 ms 596 KB Output is correct
20 Correct 67 ms 404 KB Output is correct
21 Correct 67 ms 412 KB Output is correct
22 Correct 69 ms 400 KB Output is correct
23 Correct 67 ms 360 KB Output is correct
24 Correct 68 ms 376 KB Output is correct
25 Correct 71 ms 348 KB Output is correct
26 Correct 71 ms 428 KB Output is correct
27 Correct 68 ms 432 KB Output is correct
28 Correct 66 ms 380 KB Output is correct
29 Correct 68 ms 436 KB Output is correct
30 Correct 96 ms 368 KB Output is correct
31 Correct 69 ms 380 KB Output is correct
32 Correct 64 ms 412 KB Output is correct
33 Correct 69 ms 428 KB Output is correct
34 Correct 68 ms 384 KB Output is correct
35 Correct 72 ms 360 KB Output is correct
36 Correct 76 ms 428 KB Output is correct
37 Correct 68 ms 424 KB Output is correct
38 Correct 68 ms 432 KB Output is correct
39 Correct 76 ms 428 KB Output is correct
40 Correct 66 ms 384 KB Output is correct
41 Correct 71 ms 588 KB Output is correct
42 Correct 68 ms 368 KB Output is correct
43 Correct 72 ms 548 KB Output is correct
44 Correct 73 ms 428 KB Output is correct
45 Correct 73 ms 348 KB Output is correct
46 Correct 72 ms 400 KB Output is correct
47 Correct 70 ms 432 KB Output is correct
48 Correct 73 ms 452 KB Output is correct
49 Correct 69 ms 424 KB Output is correct
50 Correct 83 ms 428 KB Output is correct
51 Correct 76 ms 364 KB Output is correct
52 Correct 73 ms 384 KB Output is correct
53 Correct 70 ms 384 KB Output is correct
54 Correct 71 ms 416 KB Output is correct
55 Correct 67 ms 432 KB Output is correct
56 Correct 68 ms 384 KB Output is correct
57 Correct 70 ms 376 KB Output is correct
58 Correct 71 ms 436 KB Output is correct
59 Correct 75 ms 432 KB Output is correct
60 Correct 66 ms 432 KB Output is correct
61 Correct 81 ms 348 KB Output is correct
62 Correct 74 ms 452 KB Output is correct
63 Correct 74 ms 436 KB Output is correct