Mikasa(位运算)

链接

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 n0,n1,,nm. 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(1t30000) — 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) (0n,m109).

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 30,31,32,33,34,35, 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 40,41,42,43,44,45,46, 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 30,31,32, 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) nx !=k (0xm)

这题可以转换为,求最小的 k k k,使得, n ⊕ x   = = k   ( m < x ) n ⊕ x ~ == k ~(m < x ) nx ==k (m<x)。由于异或运算的性质,也就是 n ⊕ k   = = x   ( m < x ) n ⊕ k ~ == x ~(m < x ) nk ==x (m<x) x x x 与一个尽量小的数字异或,使得结果大于 m m m

至此,题目的难度大大降低。

使用贪心策略,可以求得一个 k k k 使得 n ⊕ k   = = x   ( m ≤ x ) n ⊕ k ~ == x ~(m \leq x ) nk ==x (mx) (这里是 ≤ \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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值