Ehab and another another xor problem CodeForces - 1088D

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

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

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

详见https://blog.csdn.net/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;
}

猜你喜欢

转载自blog.csdn.net/sunyutian1998/article/details/84961724