# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
697699 | finn__ | Bitwise (BOI06_bitwise) | C++17 | 1 ms | 308 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
if (i && j && __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--)
{
bool all_possible = 1;
for (auto const &z : y)
all_possible = all_possible && is_possible(z, x ^ (1 << b));
if (all_possible)
x ^= 1 << b;
}
cout << x << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |