HDU 5661 Claris and XOR

8 篇文章 0 订阅
题意:

给定两个区间,在两个区间任意选择两个数,求最大的异或值?

思路:

贪心
从高位开始枚举,优先选择放置01或者10,每次判断这次的选择是否合法,然后就没了!

代码:
#include <iostream>
#include <cstdio>
#define PB push_back
#define FT first
#define SD second
#define MP make_pair
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>  P;
const int N = 233,MOD = 7+1e9;
LL a, b, c, d;
bool isok(LL x, LL y, int p)
{
    LL xr = x + (1LL << p) - 1;
    LL yr = y + (1LL << p) - 1;
    if(x > b || xr < a) return 0;
    if(y > d || yr < c) return 0;
    return 1;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T --) {
        scanf("%I64d%I64d%I64d%I64d", &a, &b, &c, &d);
        LL x = 0, y = 0, ans = 0;
        for(int i = 61;i >= 0;i --) {
            if(isok(x, y + (1LL << i), i)) {
                y += (1LL << i);
            }
            else if(isok(x + (1LL << i), y, i)) {
                x += (1LL << i);
            }
            else if(isok(x + (1LL << i), y + (1LL << i), i)) {
                y += (1LL << i);
                x += (1LL << i);
            } 
            else ;
        }
        printf("%I64d\n", x ^ y);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值