Submission #1066541

# Submission time Handle Problem Language Result Execution time Memory
1066541 2024-08-20T00:55:39 Z myst6 Ancient Machine 2 (JOI23_ancient2) C++17
0 / 100
29 ms 1376 KB
#include "ancient2.h"

#include <bits/stdc++.h>

using namespace std;

const int MAX_BITS = 1000;
const int BOUND = 51;

#define bs bitset<MAX_BITS>

vector<pair<bs,array<int,3>>> MIS(const vector<pair<bs,array<int,3>>>& vectors) {
    vector<bs> basis;  // Store the basis vectors here
    vector<pair<bs,array<int,3>>> mis;

    for (const auto& vec : vectors) {
        bs v = vec.first;  // Copy the vector

        // Try to eliminate the current vector with the basis vectors
        for (const auto& b : basis) {
            if (v.none()) break;  // If the vector is already zero, skip
            if (v.test(b._Find_first())) {  // If leading bit matches
                v ^= b;  // Subtract the basis vector (add in GF(2))
            }
        }

        // If the vector is not zero after elimination, it's linearly independent
        if (v.any()) {
            basis.push_back(v);
            mis.push_back(vec);  // Add the original vector to the mis 
        }
    }

    return mis;
}

void gaussian_elimination(vector<bs> &A, bs &S, bs &b, int N) {
    int row = 0;

    for (int col = 0; col < N; ++col) {
        // Find pivot row
        int pivot = -1;
        for (int i = row; i < N; ++i) {
            if (A[i][col]) {
                pivot = i;
                break;
            }
        }
        if (pivot == -1) continue; // No pivot found, move to the next column

        // Swap pivot row with the current row
        swap(A[row], A[pivot]);
        // swap(b[row], b[pivot]);
        int tmp = b[row];
        b[row] = b[pivot];
        b[pivot] = tmp;

        // Eliminate below
        for (int i = row + 1; i < N; ++i) {
            if (A[i][col]) {
                A[i] ^= A[row];
                // b[i] ^= b[row];
                if (b[row]) b[i] = !b[i];
            }
        }
        ++row;
    }

    // Backward substitution
    S.reset();
    for (int i = row - 1; i >= 0; --i) {
        if (A[i].any()) {
            int leading_one = A[i]._Find_first();
            S[leading_one] = b[i];
            for (int j = leading_one + 1; j < N; ++j) {
                if (A[i][j]) {
                    // S[leading_one] ^= S[j];
                    if (S[j]) S[leading_one] = !S[leading_one];
                }
            }
        }
    }
}

string solve(int N, vector<pair<bs,int>> &info) {
    // Create bitsets
    vector<bs> A(N);
    bs S, b;

    // Populate the matrix A and vector b with parity information
    for (int i=0; i<N; i++) {
        A[i] = info[i].first;
        b[i] = info[i].second;
    }

    // Perform Gaussian elimination
    gaussian_elimination(A, S, b, N);

    // Output the reconstructed binary string
    string s = S.to_string();
    reverse(s.begin(), s.end());
    return s.substr(0, N);
}

string Solve(int N) {
    vector<pair<bs,int>> info;
    function<int(int,int)> solve1 = [&](int i, int j) -> int {
        int m = 2 * j;
        vector<int> a(m), b(m);
        for (int r=0; r<j; r++) {
            a[r] = (r + 1) % j;
            b[r] = (r + 1) % j;
            a[j + r] = j + ((r + 1) % j);
            b[j + r] = j + ((r + 1) % j);
        }
        swap(b[i], b[i + j]);
        return Query(m, a, b) / j;
    }; 
    vector<int> bits(N);
    function<int(int)> solve2 = [&](int k) -> int {
        int m = k + 3;
        vector<int> a(m), b(m);
        for (int r=0; r<k; r++) {
            a[r] = r + 1;
            b[r] = r + 1;
        }
        a[k] = k + 1;
        b[k] = k + 2;
        a[k + 1] = b[k + 1] = k + 1;
        a[k + 2] = b[k + 2] = k + 2;
        return Query(m, a, b) - k - 1;
    }; 
    function<int(int)> solve3 = [&](int i) -> int {
        if (bits[i-1] == 0) {
            int nzero = 0;
            for (int j=0; j<i; j++) nzero += bits[j] == 0;
            int m = nzero + 3;
            vector<int> a(m), b(m);
            for (int r=0; r<nzero; r++) {
                a[r] = r + 1;
                b[r] = r;
            }
            a[nzero] = nzero + 1;
            b[nzero] = nzero + 2;
            return Query(m, a, b) - nzero - 1;
        } else {
            int none = 0;
            for (int j=0; j<i; j++) none += bits[j] == 1;
            int m = none + 3;
            vector<int> a(m), b(m);
            for (int r=0; r<none; r++) {
                a[r] = r + 1;
                b[r] = r;
            }
            a[none] = none + 1;
            b[none] = none + 2;
            return Query(m, a, b) - none - 1;
        }
    }; 
    vector<pair<bs,array<int,3>>> all;
    for (int i=0; i<=min(99,N); i++) {
        bs here;
        here[i] = 1;
        bits[i] = solve2(i);
        all.push_back({here, {1, i, i}});
    }
    for (int i=100; i<=min(199,N); i++) {
        bs here;
        here[i] = 1;
        bits[i] = solve3(i);
        all.push_back({here, {1, i, i}});
    }
    for (int j=1; j<=BOUND; j++) {
        for (int i=0; i<j; i++) {
            bs here;
            for (int k=i; k<N; k+=j) {
                here[k] = 1;
            }
            all.push_back({here, {0, i, j}});
        }
    }
    vector<pair<bs,array<int,3>>> mis = MIS(all);
    for (auto &[HELP, bruh] : mis) {
        if (bruh[0] == 0) {
            info.push_back({HELP, solve1(bruh[1], bruh[2])});
        } else if (bruh[0] == 1) {
            info.push_back({HELP, bits[bruh[1]]});
        } else {
            assert(false);
        }
    }
    // cout << info.size() << "\n";
    return solve(N, info);
}
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 1376 KB Wrong Answer [3]
2 Halted 0 ms 0 KB -