题目大意
给出 n , m n,m n,m求集合 { n ⨁ 0 , n ⨁ 1 , ⋯ , n ⨁ m } \{n\bigoplus0,n\bigoplus1,\cdots,n\bigoplus m\} {n⨁0,n⨁1,⋯,n⨁m}的MEX。
时间限制
1s
数据范围
n , m ≤ 1 0 9 n,m\le10^9 n,m≤109
题解
对于一个
x
x
x,如果它存在集合中,即有
n
⨁
y
=
x
,
y
≤
m
n\bigoplus y = x, y\le m
n⨁y=x,y≤m
则有
n
⨁
x
=
y
n\bigoplus x = y
n⨁x=y
显然我们就是要找到最小的
x
x
x使得
n
⨁
x
≥
m
n\bigoplus x\ge m
n⨁x≥m
对于异或,最经典的一个思路就是按位计算,从高位往低位贪心。
Code
//#pragma GCC optimize (2)
//#pragma G++ optimize (2)
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <map>
#define ll long long
#define G getchar
using namespace std;
int read()
{
char ch;
for(ch = G();ch < '0' || ch > '9';ch = G());
int n = 0;
for(;'0' <= ch && ch <= '9';ch = G())n = (n<<1)+(n<<3)+ch-48;
return n;
}
void write(int x)
{
if (x > 9)
{
write(x / 10);
putchar(x % 10 + 48);
}
else putchar(x + 48);
}
const int N = 131092;
int n , m , ans , z[32];
int main()
{
//freopen("b.in","r",stdin);
//freopen("e.out","w",stdout);
z[0] = 1;
for (int i = 1 ; i < 32 ; i++)
z[i] = z[i - 1] << 1;
for (int T = read() ; T ; T--)
{
n = read();
m = read() + 1;
ans = 0;
for (int i = 31 ; i >= 0 ; i--)
if (m & z[i])
{
if ((n & z[i]) == 0) ans = ans | z[i];
}
else
{
if (n & z[i]) break;
}
printf("%d\n", ans);
}
return 0;
}