You are given two integers n and m. Find the MEX of the sequence n ⊕ 0 , n ⊕ 1 , … , n ⊕ m n⊕0,n⊕1,…,n⊕m n⊕0,n⊕1,…,n⊕m. Here, ⊕ ⊕ ⊕ is the bitwise XOR operator.
MEX of the sequence of non-negative integers is the smallest non-negative integer that doesn’t appear in this sequence. For example, M E X ( 0 , 1 , 2 , 4 ) = 3 MEX(0,1,2,4)=3 MEX(0,1,2,4)=3, and M E X ( 1 , 2021 ) = 0 MEX(1,2021)=0 MEX(1,2021)=0.
Input
The first line contains a single integer t ( 1 ≤ t ≤ 30000 ) t (1≤t≤30000) t(1≤t≤30000) — the number of test cases.
The first and only line of each test case contains two integers n n n and m m m ( 0 ≤ n , m ≤ 1 0 9 ) (0≤n,m≤10^9) (0≤n,m≤109).
Output
For each test case, print a single integer — the answer to the problem.
Example
input
5
3 5
4 6
3 2
69 696
123456 654321
output
4
3
0
640
530866
Note
In the first test case, the sequence is 3 ⊕ 0 , 3 ⊕ 1 , 3 ⊕ 2 , 3 ⊕ 3 , 3 ⊕ 4 , 3 ⊕ 5 3⊕0,3⊕1,3⊕2,3⊕3,3⊕4,3⊕5 3⊕0,3⊕1,3⊕2,3⊕3,3⊕4,3⊕5, or 3 , 2 , 1 , 0 , 7 , 6 3,2,1,0,7,6 3,2,1,0,7,6. The smallest non-negative integer which isn’t present in the sequence i. e. the MEX of the sequence is 4 4 4.
In the second test case, the sequence is 4 ⊕ 0 , 4 ⊕ 1 , 4 ⊕ 2 , 4 ⊕ 3 , 4 ⊕ 4 , 4 ⊕ 5 , 4 ⊕ 6 4⊕0,4⊕1,4⊕2,4⊕3,4⊕4,4⊕5,4⊕6 4⊕0,4⊕1,4⊕2,4⊕3,4⊕4,4⊕5,4⊕6, or 4 , 5 , 6 , 7 , 0 , 1 , 2 4,5,6,7,0,1,2 4,5,6,7,0,1,2. The smallest non-negative integer which isn’t present in the sequence i. e. the M E X MEX MEX of the sequence is 3 3 3.
In the third test case, the sequence is 3 ⊕ 0 , 3 ⊕ 1 , 3 ⊕ 2 3⊕0,3⊕1,3⊕2 3⊕0,3⊕1,3⊕2, or 3 , 2 , 1 3,2,1 3,2,1. The smallest non-negative integer which isn’t present in the sequence i. e. the M E X MEX MEX of the sequence is 0 0 0.
思路
题意为:给出 n n n 与 m m m ,求最小的非负整数 k k k ,使得 n ⊕ x ! = k ( 0 ≤ x ≤ m ) n ⊕ x ~! = k ~(0 \leq x \leq m) n⊕x !=k (0≤x≤m)。
这题可以转换为,求最小的 k k k,使得, n ⊕ x = = k ( m < x ) n ⊕ x ~ == k ~(m < x ) n⊕x ==k (m<x)。由于异或运算的性质,也就是 n ⊕ k = = x ( m < x ) n ⊕ k ~ == x ~(m < x ) n⊕k ==x (m<x), x x x 与一个尽量小的数字异或,使得结果大于 m m m。
至此,题目的难度大大降低。
使用贪心策略,可以求得一个 k k k 使得 n ⊕ k = = x ( m ≤ x ) n ⊕ k ~ == x ~(m \leq x ) n⊕k ==x (m≤x) (这里是 ≤ \leq ≤ 而非 < < < ),可以先将 m m m 自增,再进行贪心,恰好满足题意。
#include<bits/stdc++.h>
using namespace std;
int T,a,b;
void solve(){
int ans=0;
cin>>a>>b; b++;
for(int i=31;i>=0;i--){
if((b>>i&1)==0&&(a>>i&1)==1) break;
if((b>>i&1)==1&&(a>>i&1)==0) ans|=1<<i;
}
cout<<ans<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
for(cin>>T;T;T--) solve();
}