Claris and XOR
Claris loves bitwise operations very much, especially XOR, because it has many beautiful features. He gets four positive integers a,b,c,da,b,c,d that satisfies a\leq ba≤b and c\leq dc≤d. He wants to choose two integers x,yx,y that satisfies a\leq x\leq ba≤x≤b and c\leq y\leq dc≤y≤d, and maximize the value of x~XOR~yx XOR y. But he doesn't know how to do it, so please tell him the maximum value of x~XOR~yx XOR y.
The first line contains an integer T\left(1\leq T\leq10,000\right)T(1≤T≤10,000)——The number of the test cases. For each test case, the only line contains four integers a,b,c,d\left(1\leq a,b,c,d\leq10^{18}\right)a,b,c,d(1≤a,b,c,d≤1018). Between each two adjacent integers there is a white space separated.
For each test case, the only line contains a integer that is the maximum value of x~XOR~yx XOR y.
2 1 2 3 4 5 7 13 15
6 11HintIn the first test case, when and only when x=2,y=4x=2,y=4, the value of x~XOR~yx XOR y is the maximum. In the second test case, when and only when x=5,y=14x=5,y=14 or x=6,y=13x=6,y=13, the value of x~XOR~yx XOR y is the maximum.
a b c d 结果
a b c d 结果
a b c d 结果
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 10010
#define MAXM 1001
typedef __int64 LL;
int main()
{
LL t,a,b,c,d;
cin>>t;
while(t--){
cin>>a>>b>>c>>d;
LL x = 0;
LL i = 1;
i <<= 60; //这里要注意,因为直接写1<<60得到的结果是0,因为除非大于
//2^32的数据所以就只能这样了。
for(;i;i = i>>1){
if(b >= i && c < i){ //满足优先级在后面
x += i;
b -= i;
if(a >= i) a -= i;
else a = 0;
}
else if(d >= i){
x += i;
d -= i;
if(c >= i) c -= i;
else c = 0;
if(a >= i){ //这里保证能满足大于的情况
x -= i;
a -= i;
b -= i;
}
}
}
cout<<x<<endl;
}
return 0;
}