题解:AT_arc208_c [ARC208C] Mod of XOR
此时
q=1
此时
- 若
d 是奇数或d<0 ,则q=1 的 case 无解。 - 若
d 是非负偶数,但是\left(\dfrac{d}{2}\operatorname{and} C\right)\neq \dfrac{d}{2} ,则同样无解。 - 若
d 是非负偶数,且\left(\dfrac{d}{2}\operatorname{and} C\right)=\dfrac{d}{2} ,我们令n=\left(\dfrac{d}{2}\operatorname{and} C\right)+2^{30} 即可。
q\geq 2
我们指出,若上面两种 case 都无解,则这种 case 也无解。
:::info[证明]{open}
反证法,假设
按照上述分类讨论直接实现即可,时间复杂度
:::success[代码]
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x) & -(x))
typedef long long ll;
typedef pair<int, int> pii;
template<typename T> inline void chk_min(T &x, T y) { x = min(x, y); }
template<typename T> inline void chk_max(T &x, T y) { x = max(x, y); }
int T, c, x;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> T;
while (T--) {
cin >> c >> x;
int n = c ^ x;
if (n > x) { cout << n << '\n'; continue; }
int d = c - x;
if (d >= 0 && (~d & 1)) {
d >>= 1;
if ((d & c) == d) cout << (d | (1ll << 30)) << '\n';
else cout << "-1\n";
} else cout << "-1\n";
}
return 0;
}
:::