Submission #1066549

# Submission time Handle Problem Language Result Execution time Memory
1066549 2024-08-20T01:10:04 Z myst6 Ancient Machine 2 (JOI23_ancient2) C++17
98 / 100
95 ms 1640 KB
#include "ancient2.h"
 
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
 
const int MAX_BITS = 1000;
const int BOUND = 54;
 
#define bs bitset<MAX_BITS>
 
vector<pair<bs,array<ll,3>>> MIS(const vector<pair<bs,array<ll,3>>>& vectors) {
    vector<bs> basis;  // Store the basis vectors here
    vector<pair<bs,array<ll,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(ll,int)> solve1 = [&](ll mask, 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);
            if (mask & (1LL << r)) {
                swap(b[r], b[j + r]);
            }
        }
        return Query(m, a, b) / j;
    }; 
    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;
    };
    vector<pair<bs,array<ll,3>>> all;
    for (int j=1; j<=BOUND; j++) {
        for (int i=0; i<j; i++) {
            bs here;
            for (int k=0; k<N; k++) {
                if (k % j == i) {
                    here[k] = 1;
                }
            }
            all.push_back({here, {0, 1LL << i, j}});
        }
    }
    for (int i=0; i<=min(99,N); i++) {
        bs here;
        here[i] = 1;
        all.push_back({here, {1, i, i}});
    }
    for (int j=1; j<=BOUND; j++) {
        for (int i=1; i<j; i++) {
            bs here;
            for (int k=i; k<j; k+=i) {
                here[k] = 1;
            }
            here[i] = 1;
            all.push_back({here, {2, i, j}});
        }
    }
    int A = all.size();
    vector<pair<bs,array<ll,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, solve2(bruh[1])});
        } else if (bruh[0] == 2) {
            // info.push_back({HELP, solve3(bruh[1], bruh[2])});
        } else {
            assert(false);
        }
    }
    // cout << info.size() << "\n";
    return solve(N, info);
}

Compilation message

ancient2.cpp: In function 'std::string Solve(int)':
ancient2.cpp:162:9: warning: unused variable 'A' [-Wunused-variable]
  162 |     int A = all.size();
      |         ^
# Verdict Execution time Memory Grader output
1 Partially correct 55 ms 1384 KB Output is partially correct
2 Partially correct 60 ms 1484 KB Output is partially correct
3 Partially correct 56 ms 1384 KB Output is partially correct
4 Partially correct 72 ms 1224 KB Output is partially correct
5 Partially correct 76 ms 1384 KB Output is partially correct
6 Partially correct 56 ms 1484 KB Output is partially correct
7 Partially correct 58 ms 1376 KB Output is partially correct
8 Partially correct 60 ms 1480 KB Output is partially correct
9 Partially correct 56 ms 1380 KB Output is partially correct
10 Partially correct 61 ms 1500 KB Output is partially correct
11 Partially correct 64 ms 1232 KB Output is partially correct
12 Partially correct 58 ms 1232 KB Output is partially correct
13 Partially correct 92 ms 1384 KB Output is partially correct
14 Partially correct 70 ms 1484 KB Output is partially correct
15 Partially correct 57 ms 1384 KB Output is partially correct
16 Partially correct 63 ms 1396 KB Output is partially correct
17 Partially correct 56 ms 1384 KB Output is partially correct
18 Partially correct 59 ms 1384 KB Output is partially correct
19 Partially correct 66 ms 1524 KB Output is partially correct
20 Partially correct 68 ms 1364 KB Output is partially correct
21 Partially correct 58 ms 1384 KB Output is partially correct
22 Partially correct 61 ms 1488 KB Output is partially correct
23 Partially correct 57 ms 1536 KB Output is partially correct
24 Partially correct 59 ms 1240 KB Output is partially correct
25 Partially correct 64 ms 1236 KB Output is partially correct
26 Partially correct 94 ms 1228 KB Output is partially correct
27 Partially correct 59 ms 1384 KB Output is partially correct
28 Partially correct 65 ms 1236 KB Output is partially correct
29 Partially correct 64 ms 1508 KB Output is partially correct
30 Partially correct 56 ms 1248 KB Output is partially correct
31 Partially correct 64 ms 1384 KB Output is partially correct
32 Partially correct 79 ms 1472 KB Output is partially correct
33 Partially correct 70 ms 1384 KB Output is partially correct
34 Partially correct 59 ms 1204 KB Output is partially correct
35 Partially correct 82 ms 1304 KB Output is partially correct
36 Partially correct 58 ms 1384 KB Output is partially correct
37 Partially correct 58 ms 1640 KB Output is partially correct
38 Partially correct 63 ms 1496 KB Output is partially correct
39 Partially correct 60 ms 1380 KB Output is partially correct
40 Partially correct 60 ms 1404 KB Output is partially correct
41 Partially correct 63 ms 1228 KB Output is partially correct
42 Partially correct 61 ms 1388 KB Output is partially correct
43 Partially correct 94 ms 1384 KB Output is partially correct
44 Partially correct 87 ms 1224 KB Output is partially correct
45 Partially correct 60 ms 1412 KB Output is partially correct
46 Partially correct 64 ms 1296 KB Output is partially correct
47 Partially correct 58 ms 1488 KB Output is partially correct
48 Partially correct 79 ms 1228 KB Output is partially correct
49 Partially correct 60 ms 1380 KB Output is partially correct
50 Partially correct 56 ms 1384 KB Output is partially correct
51 Partially correct 95 ms 1228 KB Output is partially correct
52 Partially correct 61 ms 1408 KB Output is partially correct
53 Partially correct 66 ms 1384 KB Output is partially correct
54 Partially correct 88 ms 1384 KB Output is partially correct
55 Partially correct 58 ms 1380 KB Output is partially correct
56 Partially correct 60 ms 1480 KB Output is partially correct
57 Partially correct 55 ms 1384 KB Output is partially correct
58 Partially correct 56 ms 1392 KB Output is partially correct
59 Partially correct 64 ms 1392 KB Output is partially correct
60 Partially correct 66 ms 1228 KB Output is partially correct
61 Partially correct 58 ms 1500 KB Output is partially correct
62 Partially correct 56 ms 1384 KB Output is partially correct
63 Partially correct 73 ms 1300 KB Output is partially correct