Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem(思维)

题目链接:https://codeforces.com/contest/1088/problem/D

 

题目大意:有a和b,可以通过? c d提问,返回a^c和b^d比较大小的结果,要求通过最多62次查询得到答案。

 

题目思路:由于数字高位能决定一个数字的大小,所以从大到小看。每一位都看,先让a的该位取反,然后让b。如果都是1的话,那么先让a取反那第一个必定小于第二个,让b取反第一个必大于第二个。如果都是0则恰巧相反。如果两个数字不同的话,就要看之前存储的一个num。两个数字大小的比较是从高位到低位第一个不同的位置开始比较。当两个数字不同时,让任一一个数字取反,该位的数字会变成相同,得到的结果是该位以后的数字的大小比较结果,也就给出了下一个两个数字不同时哪个是1。

 

以下是代码:

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
const int MAXN = 1e5+5;
const int MOD = 1e9+7;
char s[15];
int main(){
    int a,x,y;
    int ans1=0,ans2=0,big;
    printf("? 0 0\n");
    fflush(stdout);
    scanf("%d",&big);
    per(i,29,0){
        a=1<<i;
        printf("? %d %d\n",ans1+a,ans2);
        fflush(stdout);
        scanf("%d",&x);
        printf("? %d %d\n",ans1,ans2+a);
        fflush(stdout);
        scanf("%d",&y);
        if(x!=y&&x==-1){
            ans1+=a,ans2+=a;
        }
        if(x==y){
            if(big==1)ans1+=a;
            else if(big==-1) ans2+=a;
            big=x;
        }
    }
    printf("! %d %d\n",ans1,ans2);
    fflush(stdout);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值