题解:CF2162C Beautiful XOR

· · 题解

最多可以用 100 次操作是用来唬人的,根本不用这么多。

二进制从低到高按位考虑,如果第 i 位上 x 与 y 不同,想要使第 i 位上 x 与 y 相同,给 x 异或上 2^i 即可,前提是 2^i \le x,不满足的话就输出不可能即可。这样最多也不超过 32 次操作。

::::success[CODE]

#include<bits/stdc++.h>
#define int long long
#define void inline void
using namespace std;
int T;
vector<int> ans;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--) {
        ans.clear();
        int x, y;
        cin >> x >> y;
        for (int i = 0; (1ll << i) <= x; i++) {
            if (((x >> i) & 1) ^ ((y >> i) & 1)) {
                ans.push_back(1ll << i);
                x ^= 1ll << i;
            }
        }
        if (x != y) {
            cout << -1 << '\n';
            continue;
        }
        cout << ans.size() << '\n';
        for (int i : ans) cout << i << ' ';
        cout << '\n';
    }
    return 0;
}

::::