답안 #812414

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
812414 2023-08-07T08:41:34 Z piOOE Ancient Machine 2 (JOI23_ancient2) C++17
37 / 100
1091 ms 604 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 = 500;
    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;
    }

    mt19937 rnd(228);

    while (Q < 1000 && basisSize < n) {
        Q += 1;
        while (basisSize < n) {
            int p = rnd() % n + 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;
            }
        }
    }

    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) {
        assert(ans[i] != -1);
        s[i] = '0' + ans[i];
    }
    return s;
}
# 결과 실행 시간 메모리 Grader output
1 Partially correct 978 ms 480 KB Output is partially correct
2 Partially correct 935 ms 484 KB Output is partially correct
3 Partially correct 1004 ms 412 KB Output is partially correct
4 Partially correct 966 ms 480 KB Output is partially correct
5 Partially correct 1036 ms 468 KB Output is partially correct
6 Partially correct 1046 ms 516 KB Output is partially correct
7 Partially correct 978 ms 440 KB Output is partially correct
8 Partially correct 1012 ms 404 KB Output is partially correct
9 Partially correct 958 ms 488 KB Output is partially correct
10 Partially correct 935 ms 472 KB Output is partially correct
11 Partially correct 1091 ms 604 KB Output is partially correct
12 Partially correct 938 ms 440 KB Output is partially correct
13 Partially correct 914 ms 496 KB Output is partially correct
14 Partially correct 929 ms 464 KB Output is partially correct
15 Partially correct 918 ms 488 KB Output is partially correct
16 Partially correct 951 ms 480 KB Output is partially correct
17 Partially correct 962 ms 488 KB Output is partially correct
18 Partially correct 920 ms 592 KB Output is partially correct
19 Partially correct 957 ms 472 KB Output is partially correct
20 Partially correct 933 ms 488 KB Output is partially correct
21 Partially correct 928 ms 464 KB Output is partially correct
22 Partially correct 921 ms 464 KB Output is partially correct
23 Partially correct 927 ms 380 KB Output is partially correct
24 Partially correct 918 ms 416 KB Output is partially correct
25 Partially correct 937 ms 448 KB Output is partially correct
26 Partially correct 941 ms 592 KB Output is partially correct
27 Partially correct 929 ms 464 KB Output is partially correct
28 Partially correct 926 ms 384 KB Output is partially correct
29 Partially correct 915 ms 396 KB Output is partially correct
30 Partially correct 932 ms 488 KB Output is partially correct
31 Partially correct 942 ms 464 KB Output is partially correct
32 Partially correct 915 ms 464 KB Output is partially correct
33 Partially correct 942 ms 464 KB Output is partially correct
34 Partially correct 984 ms 464 KB Output is partially correct
35 Partially correct 981 ms 492 KB Output is partially correct
36 Partially correct 958 ms 468 KB Output is partially correct
37 Partially correct 928 ms 400 KB Output is partially correct
38 Partially correct 928 ms 484 KB Output is partially correct
39 Partially correct 921 ms 424 KB Output is partially correct
40 Partially correct 916 ms 488 KB Output is partially correct
41 Partially correct 915 ms 488 KB Output is partially correct
42 Partially correct 929 ms 484 KB Output is partially correct
43 Partially correct 933 ms 480 KB Output is partially correct
44 Partially correct 926 ms 480 KB Output is partially correct
45 Partially correct 937 ms 484 KB Output is partially correct
46 Partially correct 954 ms 484 KB Output is partially correct
47 Partially correct 950 ms 464 KB Output is partially correct
48 Partially correct 934 ms 464 KB Output is partially correct
49 Partially correct 919 ms 600 KB Output is partially correct
50 Partially correct 931 ms 444 KB Output is partially correct
51 Partially correct 934 ms 480 KB Output is partially correct
52 Partially correct 927 ms 488 KB Output is partially correct
53 Partially correct 934 ms 464 KB Output is partially correct
54 Partially correct 939 ms 464 KB Output is partially correct
55 Partially correct 914 ms 464 KB Output is partially correct
56 Partially correct 947 ms 432 KB Output is partially correct
57 Partially correct 914 ms 592 KB Output is partially correct
58 Partially correct 912 ms 476 KB Output is partially correct
59 Partially correct 914 ms 492 KB Output is partially correct
60 Partially correct 943 ms 484 KB Output is partially correct
61 Partially correct 927 ms 552 KB Output is partially correct
62 Partially correct 928 ms 456 KB Output is partially correct
63 Partially correct 926 ms 408 KB Output is partially correct