题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6702
题解: 由于目的是使 (A xor C) & (B xor C)
的值尽量小,那么 A xor C
和 B xor C
的值在二进制形式下的1数量要尽量少。又因为 1 xor 1 = 0
,所以可以取 A 和 B 中在二进制形式下出现的所有1,即 C = A & B
,注意特判为0的情况。
代码及注释如下:
#include<iostream>
using namespace std;
long long a,b,c;
int main() {
int t;
cin>>t;
while(t--) {
cin>>a>>b;
c=a&b;//取a与b两者中都有的1,在异或运算中会将1转为0
if(c==0) cout<<"1"<<endl;
else cout<<c<<endl;
}
return 0;
}