CF1554C Solution

题意:给出两个整数 \(n\)\(m\),求 \(\operatorname{mex}\{n \oplus 0,n \oplus 1,\dots,n \oplus m\}\)

由于异或的神秘特质逆运算是它本身,我们可以将其转化为求最小的 \(c\) 使得 \(n\oplus c\ge m+1\)

那么就有一种从高到低按位贪心的构造方法:

  • 如果 \(n\)\(m+1\) 在这一位上相等,\(c\) 的这一位为 \(0\)
  • 如果 \(n\) 的这一位为 \(1\),而 \(m+1\) 的这一位为 \(0\),那么 \(c\) 的剩余位全是 \(0\)
  • 否则 \(c\) 该位为 \(1\)

这样我们就能构造出最小的 \(c\) 使得 \(n\oplus c\ge m+1\)

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b,c;
void man(){
	cin>>a>>b;
	c=0;
	b++;
	for(int i=30;i>=0;i--){
		int x=((a>>i)&1);
		int y=((b>>i)&1);
		if(x==y) continue;
		if(x==1&&y==0) break;
		c^=(1<<i);
	}
	cout<<c<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T;cin>>T;
	while(T--) man();
	return 0;
}
posted @ 2026-05-22 20:21  NiYqaq  阅读(8)  评论(0)    收藏  举报