답안 #697701

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
697701 2023-02-10T19:12:24 Z finn__ Bitwise (BOI06_bitwise) C++17
100 / 100
1 ms 212 KB
#include <bits/stdc++.h>
using namespace std;

// Whether x can be built from elements in z.
bool is_possible(vector<pair<unsigned, unsigned>> z, unsigned x)
{
    if (!x)
        return 1;

    // Incorporate all "fixed" bits of elements in z, as they will always be
    // present. Must be taken out since not all bits afterwards are arbitrarily
    // variable.
    for (auto &[i, j] : z)
        while (i && __builtin_clz(i) == __builtin_clz(j))
        {
            unsigned const b = 31 - __builtin_clz(i);
            i ^= 1 << b, j ^= 1 << b;
            if (x & (1 << b))
                x ^= 1 << b;
        }

    if (!x)
        return 1;

    // See if one element covers all remaining bits of x.
    for (auto const &[i, j] : z)
        if (j && __builtin_clz(j) < __builtin_clz(x))
            return 1;

    // See if two elements cover all remaining bits of x.
    unsigned c = 0;
    for (auto const &[i, j] : z)
        if (j)
            c += __builtin_clz(j) == __builtin_clz(x);
    if (c >= 2)
        return 1;

    // Select the unique (=> branching factor 1) element to cover the missing
    // bit and solve for the remaining bits.
    for (auto &[i, j] : z)
        if (j && __builtin_clz(j) == __builtin_clz(x))
        {
            unsigned const b = 31 - __builtin_clz(j);
            i = 0, j ^= 1 << b, x ^= 1 << b;
            return is_possible(z, x);
        }

    return 0;
}

int main()
{
    size_t n, p;
    cin >> n >> p;

    vector<vector<pair<unsigned, unsigned>>> y(p);
    for (size_t i = 0; i < p; i++)
    {
        size_t k;
        cin >> k;
        y[i] = vector<pair<unsigned, unsigned>>(k);
    }
    for (size_t i = 0; i < p; i++)
        for (auto &[a, b] : y[i])
            cin >> a >> b;

    unsigned x = 0;
    for (unsigned b = 30; b < 31U; b--)
    {
        x ^= 1 << b;
        for (auto const &z : y)
            if (!is_possible(z, x))
            {
                x ^= 1 << b;
                break;
            }
    }

    cout << x << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct