神秘小构造。

观察式子 (n \oplus c) \bmod n = x ,左部仅可能为:n \oplus c 或 n \oplus c - n。

所以可以考虑 n 是否取模。

  1. 若不取模,则 n \oplus c < n,则 n \oplus c = x,即 n = x \oplus c,于是若 x \oplus c > x 时,n 必定有解。
  2. 若取模,则 n \oplus c = n + x,考虑解这个方程。

异或等于二进制不进位加法。注意到等式两侧为异或和加法。可以从进位角度思考。

因为 a \oplus b \le a + b,则 c < x 时必定无解。

否则,考虑构造一个 n \oplus c = n + x 的 n。

因为加法存在进位,于是从低位往高位考虑。

记 f(x, i) 表示 x 二进制中第 i 位的数字。

若当前的 f(n + x, i) \ne f(n \oplus c, i),那么 n 这一位取任何值都不能使等式成立。

否则,考虑 f(n + x, i) 的值。

若为 0,不能发生进位,无法影响到下一位。

若为 1,当 f(n + x, i + 1) \ne f(n \oplus c, i + 1),n 的第 i 位取 1 能够使第 i + 1 位合法,否则取 0。

处理完所有位数后,可能存在 n < x 的情况,此时 n 加上 2^{35} 后对方程的成立不会有影响,所以可以构造出一个合法的 n。

:::info[code]

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int, int> pi;
#define endl '\n'
bool check (ll n, int x, int c) {
	return (n ^ c) % n == x;
}

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while (t --) {
		int x, c;
		cin >> c >> x;
		if ((x ^ c) > x) {
			cout << (x ^ c) << endl;
		} else {
			if (c < x) { // n ^ c <= n + c -> n + c <= n + x -> 无解 
				cout << -1 << endl;
			} else { // n ^ c = x + n
				ll n = (1ll << 35);
				bool flag = 1;
				for (int i = 0; i < 30; i ++) {
					int v1 = (c >> i & 1), v2 = (x >> i & 1);
					int u1 = (c >> (i + 1) & 1), u2 = (x >> (i + 1) & 1);
					if (v1 != v2) {
						flag = 0;
						break;
					}
					if (v1) {
						if (u1 != u2) n += (1 << i), x += (1 << i + 1); // 加法进位 
					}
				}
//				cout << check(n, x, c) << endl;
				if(flag) cout << n << endl;
				else cout << -1 << endl;
			}
		}
	}
	return 0;
}

:::