Ehab and another another xor problem CodeForces - 1088D

本文介绍了一种使用位运算解决特定问题的算法策略,通过对比两个数的二进制表示来判断大小,适用于无法直接使用二分搜索的情况。文章详细解释了如何逐位比较并调整两个数的值以找到正确答案,提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://codeforces.com/contest/1088/problem/D

位运算没法二分 只能按位考虑

判断两个数大小 从高位扫向低位 只要有一位不等就分出大小

详见https://blog.youkuaiyun.com/xs18952904/article/details/84803490

 

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e3+10;

int pre[50];

void init()
{
    int i;
    pre[0]=1;
    for(i=1;i<=30;i++) pre[i]=2ll*pre[i-1];
}

int judge(int c,int d)
{
    int res;
    printf("? %d %d\n",c,d);
    fflush(stdout);
    scanf("%d",&res);
    return res;
}

int main()
{
    int i,a,b,flag,x,y;
    init();
    a=0,b=0;
    if(judge(0,0)>=0) flag=1;
    else flag=0;
    for(i=29;i>=0;i--){
        x=judge(a^pre[i],b),y=judge(a,b^pre[i]);
        if(x==y){//not equal
            if(flag) a+=pre[i];
            else b+=pre[i];
            if(x==1) flag=1;
            else flag=0;
        }
        else{
            if(x==-1) a+=pre[i],b+=pre[i];
        }
    }
    printf("! %d %d\n",a,b);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值