C. Mikasa

题目:https://codeforces.com/contest/1554/problem/C

You are given two integers n and m. Find the MEX of the sequence 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, MEX(0,1,2,4)=3, and MEX(1,2021)=0.

Input
The first line contains a single integer t (1≤t≤30000) — the number of test cases.

The first and only line of each test case contains two integers n and m (0≤n,m≤109).

Output
For each test case, print a single integer — the answer to the problem.

Example
inputCopy
5
3 5
4 6
3 2
69 696
123456 654321
outputCopy
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, or 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.

In the second test case, the sequence is 4⊕0,4⊕1,4⊕2,4⊕3,4⊕4,4⊕5,4⊕6, or 4,5,6,7,0,1,2. The smallest non-negative integer which isn’t present in the sequence i. e. the MEX of the sequence is 3.

In the third test case, the sequence is 3⊕0,3⊕1,3⊕2, or 3,2,1. The smallest non-negative integer which isn’t present in the sequence i. e. the MEX of the sequence is 0.

题解证明:
How can we check if k is present in the sequence n⊕0,n⊕1,…,n⊕m ? Think.

If k is present in the sequence, then there must be some x such that 0≤x≤m and n⊕x=k, right?

Did you know that n⊕k=x is equivalent to n⊕x=k ?

So we can just check if n⊕k≤m or not! Pretty simple!

So the modified problem is to find the smallest non-negative integer k such that n⊕k≥m+1. Can you solve it now?

Think using bits.

Let p=m+1 and ti be the i-th bit of t. We will find the smallest k such that n⊕k≥p.

Let’s build k greedily from the highest bit to the lowest bit. Let’s say we will find the i-th bit of k and the higher bits have already been generated. Obviously, we will try to make this bit off if possible. When will it be impossible? Think.

If ni=pi, we can set ki=0 as ni⊕0=ni≤pi. If ni=1 and pi=0, we can break here by setting the remaining bits of k off as no matter what the remaining bits of n are, n⊕k will always be greater than p. Finally, if ni=0 and pi=1, we must set ki=1, as we have no other options.

Check my solution for more clarity.

Time Complexity: O(log(n)) per test case.

。。太难做了。。

AC代码:

#include <iostream>
using namespace std;

int main(){
	
	ios_base::sync_with_stdio(false);
	
	int t; cin >> t;
	
	while(t--){
		
		int n,m;
		cin >> n >> m;
		++m;
		
		int ans = 0;
		
		for(int i = 30;i>=0 && n < m; i--){
			
			if(((n >> i)&1) == ((m >> i)&1)) continue;
			if((m >> i)&1) ans |= 1 << i,n |= 1 << i;
		}
		cout << ans << endl; 
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值