C. Beautiful XOR

Problem Statement

You are given two integers aa and bb. You are allowed to perform the following operation any number of times (including zero):

  • choose any integer xx such that 0xa0 \le x \le a (the current value of aa, not initial),
  • set a:=axa := a \oplus x. Here, \oplus represents the bitwise XOR operator.

After performing a sequence of operations, you want the value of aa to become exactly bb.

Find a sequence of at most 100 operations (values of xx used in each operation) that transforms aa into bb, or report that it is impossible.

Note that you are not required to find the minimum number of operations, but any valid sequence of at most 100 operations.

Solution

Let the initial value be aa and the target value be bb. After a series of operations with chosen integers x1,x2,,xkx_1, x_2, \ldots, x_k, the final value of aa will be ax1x2xka \oplus x_1 \oplus x_2 \oplus \ldots \oplus x_k. Let X=x1x2xkX = x_1 \oplus x_2 \oplus \ldots \oplus x_k. We want the final value to be bb, so we have the equation: aX=ba \oplus X = b This implies that the total XOR sum of the operations must be X=abX = a \oplus b.

Our task is to find a sequence of xix_i values that are valid (i.e., xiacurrentx_i \le a_{\text{current}} at each step) and whose XOR sum is X=abX = a \oplus b.

A simple way to construct the total XOR sum XX is to break it down into its constituent powers of 2. Any integer can be uniquely represented as a sum of distinct powers of 2, which correspond to the set bits in its binary representation. If we perform an XOR operation for each of these powers of 2, their total XOR sum will be equal to XX.

Let’s say X=p1p2pkX = p_1 \oplus p_2 \oplus \ldots \oplus p_k, where each pip_i is a distinct power of 2. We can use this sequence of pip_i as our operations xix_i. The condition for each operation is that piacurrentp_i \le a_{\text{current}}.

Let’s analyze this condition. Let pmaxp_{\max} be the largest power of 2 in the binary representation of XX. This corresponds to the most significant bit where aa and bb differ.

Case 1: pmaxap_{\max} \le a (the initial value) If the largest required power of 2, pmaxp_{\max}, is less than or equal to the initial value of aa, a solution is possible. Let the required operations be p1,p2,,pkp_1, p_2, \ldots, p_k where pk=pmaxp_k = p_{\max}. At any step, when we perform an operation anew=aoldpia_{\text{new}} = a_{\text{old}} \oplus p_i, we need to ensure piaoldp_i \le a_{\text{old}}. Since pmaxp_{\max} is the most significant bit of difference, all bits of aa at positions higher than pmaxp_{\max} are the same as in bb. The condition pmaxap_{\max} \le a implies that the most significant bit of aa is at least at the same position as pmaxp_{\max}. When we apply operations with pi<pmaxp_i < p_{\max}, these operations only affect bits lower than the most significant bit of aa. Therefore, the value of aa remains greater than or equal to pmaxp_{\max}. This ensures that acurrentpmaxpia_{\text{current}} \ge p_{\max} \ge p_i for any subsequent operation pip_i. Thus, all operations in the sequence are valid.

Case 2: pmax>ap_{\max} > a (the initial value) If the largest power of 2 required for the transformation, pmaxp_{\max}, is greater than the initial aa, it is impossible to reach bb. Let pmax=2mp_{\max} = 2^m. The condition pmax>ap_{\max} > a means that a<2ma < 2^m. This implies that all bits of aa at or above position mm are 0. For any operation, we must choose an xacurrentx \le a_{\text{current}}. If acurrent<2ma_{\text{current}} < 2^m, then we must also have x<2mx < 2^m. The result of the XOR operation, acurrentxa_{\text{current}} \oplus x, will also be less than 2m2^m. This is because the XOR of two numbers cannot produce a result with a more significant bit than the larger of the two numbers. Since we start with a<2ma < 2^m, we can never reach a value greater than or equal to 2m2^m. However, since pmax=2mp_{\max}=2^m is part of X=abX = a \oplus b, the mm-th bit of aa and bb must differ. As the mm-th bit of aa is 0, the mm-th bit of bb must be 1. This means b2mb \ge 2^m. Since we can never reach a value 2m\ge 2^m, it is impossible to transform aa into bb.

So, the strategy is:

  1. Calculate X=abX = a \oplus b.
  2. If X=0X=0, no operations are needed.
  3. Find the largest power of 2 in XX, let it be pmaxp_{\max}.
  4. If pmaxap_{\max} \le a, the solution is the sequence of all powers of 2 that constitute XX.
  5. If pmax>ap_{\max} > a, no solution exists.
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 a, b;
    cin >> a >> b;
    int x = a ^ b;
    vector<int> ans;
    int pow = 1;
    if (x == 0){
        cout << 0 << endl;
        return;
    }
    while(x > 0) {
        if(x & 1) {
            ans.push_back(pow);
        }
        pow *= 2;
        x >>= 1;
    }
    if (ans.back() <= a ) {
        cout << ans.size() <<endl;
        for (int i = 0; i < ans.size(); i++) {
            cout << ans[i] << " ";
            }
        cout << endl;
    } else {
        cout << -1 << 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();
    }
}