B. Bitwise Reversion

Problem Statement

You are given three non-negative integers xx, yy and zz. Determine whether there exist three non-negative integers aa, bb and cc satisfying the following three conditions:

  • a & b=xa \ \& \ b = x
  • b & c=yb \ \& \ c = y
  • a & c=za \ \& \ c = z

where &\& denotes the bitwise AND operation.

Solution Explanation

The problem requires us to find if three non-negative integers a,b,ca, b, c exist, given the results of their pairwise bitwise AND operations.

Let’s analyze the constraints imposed by the bitwise AND operation on a,b,ca, b, c.

  • The condition a & b=xa \ \& \ b = x implies that for any bit position, if the bit is 1 in xx, it must also be 1 in both aa and bb.
  • Similarly, b & c=yb \ \& \ c = y implies that any bit set in yy must also be set in both bb and cc.
  • And a & c=za \ \& \ c = z implies that any bit set in zz must also be set in both aa and cc.

From these observations, we can determine the minimum set of bits that must be active in a,b,ca, b, c.

  • For aa, it must contain all the bits that are set in xx and all the bits that are set in zz. The smallest integer that satisfies this is x  zx \ | \ z.
  • For bb, it must contain all the bits from xx and yy. The smallest integer is x  yx \ | \ y.
  • For cc, it must contain all the bits from yy and zz. The smallest integer is y  zy \ | \ z.

This gives us a potential candidate solution. Let’s try constructing a,b,ca, b, c this way:

  • Let a=x  za = x \ | \ z
  • Let b=x  yb = x \ | \ y
  • Let c=y  zc = y \ | \ z

This construction gives us the “smallest” possible values for a,b,ca, b, c that could work. If any other integers a,b,ca', b', c' form a valid solution, it must be that aa is a submask of aa', bb is a submask of bb', and cc is a submask of cc'. However, adding more bits to a,b,ca, b, c (i.e., changing a 0 to a 1) can only cause the result of an AND operation to stay the same or change from 0 to 1. It can never change a 1 to a 0. This means if our minimal construction (a & b) results in a bit that is not in x, no larger value for a or b can fix it.

Therefore, if this minimal construction works, a solution exists. If it fails, no solution can exist. The strategy is to construct these candidate values for a,b,ca, b, c and then verify if they satisfy the original three equations.

The algorithm is:

  1. Calculate the candidate values: a = x | z, b = x | y, c = y | z.
  2. Check if they satisfy the conditions:
    • (a & b) == x
    • (b & c) == y
    • (c & a) == z
  3. If all three conditions are true, the answer is “YES”. Otherwise, it’s “NO”.
View Code
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
#define int long long
// #define mod 1000000007

#define N 500005
void solve() {
    int x, y, z;
    cin >> x >> y >> z;

    int a = x | z;
    int b = x | y;
    int c = y | z;
    if ((a & b) == x && (b & c) == y && (c & a) == z){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef DEBUG
    freopen("./input.txt", "r", stdin);
#endif
    int t;
    cin >> t;
    while (t--) {
#ifdef DEBUG
        static int test_num = 1;
        cout << "test case: " << test_num++ << endl;
#endif
        solve();
    }
}