本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
Codeforces Round #735 (Div. 2)-C. Mikasa
传送门 Time Limit: 1 second Memory Limit: 256 megabytes
Problem Description
You are given two integers and . Find the of the sequence . Here, is the bitwise XOR operator.
of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, , and .
Input
The first line contains a single integer () — the number of test cases.
The first and only line of each test case contains two integers and ().
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 , or . The smallest non-negative integer which isn't present in the sequence i. e. the of the sequence is .
In the second test case, the sequence is , or . The smallest non-negative integer which isn't present in the sequence i. e. the of the sequence is .
In the third test case, the sequence is , or . The smallest non-negative integer which isn't present in the sequence i. e. the of the sequence is .
题目大意
给你两个数和,用所有到的数去异或。
在得到的结果中,输出最小的未出现过的非负正整数。
解题思路
假设,那么根据异或的性质就有。
我们可以尝试一些,用去异或,看得到的是否属于~。如果属于,那么这个就能够被~中的某个数异或来得到;如果不属于,那么这个就是~中的数异或的所有结果中未出现过的。
所以现在就是在所有的异或之后的结果不属于~的中,找到最小的。问题转化为:找到最小的使得。
接下来我们从高到低来看的每一位就行了。
想让最小,那么这一位能够是的话绝不是。
如果和的这一位和相同,那么的这一位就可以是,因为
如果
如果是而是,那么可以是,因为
如果是而是的话,那么必须是,否则就了。
任何时刻,一旦已经了,剩下的位都为就可以了,之间掉就行。
具体实现可以再参考一下代码。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main()
{
int N;
cin>>N;
while(N--)
{
int n, m;
cd(n), cd(m); // scanf n、m
m++; // m 变成 m+1
int k=0; // 答案k的初始值为0(越小越好)
for(int i=30;i>=0;i--) // 枚举每一位
{
int thisN=n>>i&1; // n的这一位
int thisM=m>>i&1; // m的这一位
if(thisN!=thisM) // 如果相等就什么都不需要做,如果不相等:
{
if(thisN==0) // 如果n的这一位是0(说明m的这一位是1),那么k的这一位必须是1
{
k+=1<<i;
}
}
if((n^k)>=m)break; // 一旦n异或k已经大于等于m了(现在的m是输入的m+1),就退出
}
cout<<k<<endl; // 输出k即可
}
return 0;
}
前面想歪了
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:letmefly.blog.csdn.net/article/det…