# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
697699 |
2023-02-10T19:00:43 Z |
finn__ |
Bitwise (BOI06_bitwise) |
C++17 |
|
1 ms |
308 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)
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 |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
308 KB |
Output isn't correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Incorrect |
1 ms |
244 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |