Codeforces Round #735 (Div. 2)-C. Mikasa-题解
Posted Tisfy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #735 (Div. 2)-C. Mikasa-题解相关的知识,希望对你有一定的参考价值。
目录
Codeforces Round #735 (Div. 2)-C. Mikasa
传送门
Time Limit: 1 second
Memory Limit: 256 megabytes
Problem Description
You are given two integers n n n and m m m. Find the MEX \\operatorname{MEX} MEX of the sequence n ⊕ 0 , n ⊕ 1 , … , n ⊕ m n \\oplus 0, n \\oplus 1, \\ldots, n \\oplus m n⊕0,n⊕1,…,n⊕m. Here, ⊕ \\oplus ⊕ is the bitwise XOR operator.
MEX \\operatorname{MEX} 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 \\operatorname{MEX}(0, 1, 2, 4) = 3 MEX(0,1,2,4)=3, and MEX ( 1 , 2021 ) = 0 \\operatorname{MEX}(1, 2021) = 0 MEX(1,2021)=0.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 30 000 1 \\le t \\le 30\\,000 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 \\le n, m \\le 10^9 0≤n,m≤109).
Output
For each test case, print a single integer — the answer to the problem.
Sample Input
5
3 5
4 6
3 2
69 696
123456 654321
Sample Onput
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 \\oplus 0, 3 \\oplus 1, 3 \\oplus 2, 3 \\oplus 3, 3 \\oplus 4, 3 \\oplus 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 \\operatorname{MEX} 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 \\oplus 0, 4 \\oplus 1, 4 \\oplus 2, 4 \\oplus 3, 4 \\oplus 4, 4 \\oplus 5, 4 \\oplus 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 MEX \\operatorname{MEX} MEX of the sequence is 3 3 3.
In the third test case, the sequence is 3 ⊕ 0 , 3 ⊕ 1 , 3 ⊕ 2 3 \\oplus 0, 3 \\oplus 1, 3 \\oplus 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 MEX \\operatorname{MEX} MEX of the sequence is 0 0 0.
题目大意
给你两个数 n n n和 m m m,用所有 0 0 0到 m m m的数 x x x去异或 n n n。
在得到的结果中,输出最小的未出现过的非负正整数。
解题思路
假设 x ⊕ n = k x\\oplus n=k x⊕n=k,那么根据异或的性质就有 n ⊕ k = x n\\oplus k=x n⊕k=x。
我们可以尝试一些 k k k,用 k k k去异或 n n n,看得到的 x x x是否属于 0 0 0~ m m m。如果属于,那么这个 k k k就能够被 0 0 0~ m m m中的某个数异或 n n n来得到;如果不属于,那么这个 k k k就是 0 0 0~ m m m中的数异或 n n n的所有结果中未出现过的。
所以现在就是在所有的异或 n n n之后的结果不属于 0 0 0~ m m m的 k k k中,找到最小的 k k k。问题转化为:找到最小的 k k k使得 k ⊕ n ≥ m + 1 k\\oplus n\\geq m+1 k⊕n≥m+1。
接下来我们从高到低来看 k k k的每一位就行了。
想让 k k k最小,那么这一位能够是 0 0 0的话绝不是 1 1 1。
如果 n n n和 m + 1 m+1 m+1的这一位 n i n_i ni和 ( m + 1 ) i (m+1)_i (m+1)i相同,那么 k k k的这一位就可以是 0 0 0,因为 0 ⊕ n i = n i = ( m + 1 ) i ≥ ( m + 1 ) i 0\\oplus n_i = n_i = (m+1)_i\\geq(m+1)_i 0⊕ni=ni=(m+1)i≥(m+1)i
如果 n i ≠ ( m + 1 ) i n_i\\neq (m+1)_i ni=(m+1)i
如果 n i n_i ni是 1 1 1而 ( m + 1 ) i (m+1)_i (m+1)i是 0 0 0,那么 k i k_i ki可以是 0 0 0,因为 k i ⊕ n i = 0 ⊕ 1 = 1 > 0 = ( m + 1 ) i k_i\\oplus n_i=0\\oplus1=1>0=(m+1)_i ki⊕ni=0⊕1=1>0=(m+1)Codeforces Round #735 (Div. 2)-C. Mikasa-题解
Codeforces Round #735 (Div. 2)-B. Cobb-题解
Codeforces Round #735 (Div. 2)-A. Cherry-题解
Codeforces Round #735 (Div. 2)-C. Mikasa-题解