hdu 5661 Claris and XOR

Claris and XOR

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 744    Accepted Submission(s): 330


Problem Description
Claris loves bitwise operations very much, especially XOR, because it has many beautiful features. He gets four positive integers  a,b,c,d that satisfies ab and cd. He wants to choose two integers x,y that satisfies axb and cyd, and maximize the value of x XOR y. But he doesn't know how to do it, so please tell him the maximum value of x XOR y.
 

 

Input
The first line contains an integer  T(1T10,000)——The number of the test cases.
For each test case, the only line contains four integers 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 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=4$, the value of $x~XOR~y$ is the maximum. In the second test case, when and only when $x=5,y=14$ or $x=6,y=13$, the value of $x~XOR~y$ is the maximum.
 

数据范围是10^18,转化为二进制是2^62左右,检查每一位的取值
1.如果只能取1那就取
2.如果可以取1或者取零那么就让x,y中的一方取0,另一方取1

#include <cstdio>
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        long long a,b,c,d;
        scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
        long long ans,ansx=0,ansy=0;
        for(int i=62;i>=0;i--)
        {
            ans=1LL<<i;
            if(ansx+ans<=b&&ansy+ans<=d)
            {
                if((ansx+ans<a)||(ansy+ans<c))   //必选,不然无法出现在范围内
                {
                  if(ansx+ans<a) ansx+=ans;
                  if(ansy+ans<c) ansy+=ans;
                }
                else
                {
                   ansx+=ans;   //可选其中之一
                }
            }
            else
            {
                if(ansx+ans<=b) ansx+=ans;
                else if(ansy+ans<=d) ansy+=ans;
            }
        }
        printf("%lld\n",ansx^ansy);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/zsyacm666666/p/6219561.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值