bestcoder 79 Claris and XOR

3 篇文章 0 订阅

Claris and XOR

 
 Accepts: 61
 
 Submissions: 201
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
Problem Description

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 bab and c\leq dcd. He wants to choose two integers x,yx,y that satisfies a\leq x\leq baxb and c\leq y\leq dcyd, 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.

Input

The first line contains an integer T\left(1\leq T\leq10,000\right)T(1T10,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(1a,b,c,d1018). Between each two adjacent integers there is a white space separated.

Output

For each test case, the only line contains a integer that is the maximum value of x~XOR~yx XOR y.

Sample Input
2
1 2 3 4
5 7 13 15
Sample Output
6
11


     
     
Hint
In 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.

思路:贪心

从最高位往低位贪心,当没有办法满足大于最低的情况只能都取,当c小于当前贪心的位置的时候能不选择就不选

例如
十进制  5 7 13   15
二进制 101 111 1101  1111
当进行到2^3位时我们就可以取的15的2^8

a b c d 结果
十进制  5 7 5   7 8
二进制 101 111 101  111 1000

之后找到2^2位我们在b处取得100,但是因为c > 2^2所以如果不取得这一位就无法满足大于C所以在这里也要取100
a b c d 结果
十进制  1 3 1   3 8
二进制 1 11 1 11 1000

之后再次进行贪心在b处取得10,当前位置不会让y不满足大于c所以就不取

a b c d 结果
十进制  0 1 1   3 10
二进制 0 1 1 11 1010

现在对2^0次方进行贪心,但是因为a<当前贪心的位置也就是2^0,所以能不取就不取,然后我们就取得d中的1

a b c d 结果
十进制  0 1 0   2 11
二进制 0 1 0 10 1011

其实我也是强行憋出来的很多东西我也不太能用语言表达出来我就直接贴代码了


#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;
}



十进制  5 7 13   15
二进制 101 111 1101  1111
a b c d 结果
十进制  1 3 1   3 8
二进制 1 11 1 11 1000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值