Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Bitwise AND has no carries, so each bit position can be treated completely independently. Stop thinking about the whole number at once.
For one bit, you only need to decide whether there exist bits that create the target bits .
If , then both and must be . If , then both and must be . If , then both and must be .
The impossible case is when exactly two of are . Example: if but , then is forced, which makes anyway. Oops.
A clean way to test everything: try the forced minimal construction , , , then just verify the three AND equations.
Bitwise AND is a lossy operation. It does not have some cute arithmetic inverse hiding in the bushes. The whole trick is to stop trying to reverse the full numbers and instead look at one bit at a time.
For every bit position, we need three bits such that:
Since bitwise AND never mixes different bit positions, this local condition is enough. If every bit position is possible, the whole numbers are possible.
What does a target bit force?
If , then , so both and .
If , then both and .
If , then both and .
Now notice the annoying case: exactly two of are .
Suppose , , and .
From , we need and .
From , we need and .
So now and , which forces:
That means must be , contradiction. So is impossible.
By symmetry, and are also impossible.
The possible target patterns for one bit are:
So the entire condition is:
No bit may be set in exactly two of .
There are two nice ways to implement this.
Direct bad-bit check
A bit is bad if it appears in exactly two numbers:
If that expression is nonzero, answer NO; otherwise answer YES.
Even cleaner construction check
Every bit that appears in must be in both and .
Every bit that appears in must be in both and .
Every bit that appears in must be in both and .
So the forced minimal candidates are:
Then just check whether these actually work:
If they work, answer YES. If not, some forbidden exactly-two-ones bit created an extra AND bit, so answer NO.
That is the whole problem. Tiny bitmask logic, zero drama.
Complexity: per test case.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll x, y, z;
cin >> x >> y >> z;
ll a = x | z;
ll b = x | y;
ll c = y | z;
if ((a & b) == x && (b & c) == y && (a & c) == z) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll x, y, z;
cin >> x >> y >> z;
ll a = x | z;
ll b = x | y;
ll c = y | z;
if ((a & b) == x && (b & c) == y && (a & c) == z) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}