Codeforces Round #525 (Div. 2) D - Ehab and another another xor problem 交互题

题目链接

题意:交互题,要求猜两个数(a,b),每次可以询问c,d,根据 (a^c) 和 (b^d) 的大小关系给出 1  0  -1 三种状态,要求根据这些状态求出a和b的值。询问次数小于等于62,a和b的值小于2的30次方。

 Solution:先输出  "? 0 0"  得出a和b的大小关系,然后根据异或计算的性质计算a和b二进制的1每一位,最终的出结果,具体看代码。

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<cstdlib>
#define ll long long
#define ull unsigned long long
#define mem(a,x) memset((a),(x),sizeof ((a)))//x只能是0或-1或false或true
#define debug(x) cout<<"X: "<<(x)<<endl
#define de cout<<"************"<<endl
#define lowbit(x) ((x)&(-x))
#define lson rt<<1
#define rson rt<<1|1
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/(__gcd(a,b))
#define inf 0x3f3f3f3f//1e9+6e7
#define eps 1e-8
#define mod 1e9+7
#define N 1000010
const double pi=acos(-1.0);
using namespace std;
int main()
{
    int a,b,c,d;
    a=0;
    b=0;
    cout<<"? 0 0"<<endl;
    fflush(stdout);
    int ff;
    cin>>ff;
    int f;
    for(int i=29; i>=0; i--)
    {
        if(ff==0)   //如果(a^c) 和 (b^d) 相等
        {
            cout<<"? "<<(a|(1<<i))<<" "<<b<<endl;   //相当于让a的第i位和1异或,b的第i位和0异或
            fflush(stdout);
            cin>>f;
            if(f==-1)//a和b的第i位都是1
                {a|=(1<<i);b|=(1<<i);}
        }
        else    //(a^c) 和 (b^d) 不相等
        {
            cout<<"? "<<(a|(1<<i))<<" "<<(b|(1<<i))<<endl;//相当于让a和b的第i位都和1异或
            fflush(stdout);
            cin>>f;
            if(f!=ff)   //如果(a^c) 和 (b^d) 的大小关系发生了变化
            {
                if(f==1)    //a的第i位是0,b的第i位是1
                    b|=(1<<i);
                else        //a的第i位是1,b的第i位是0
                    a|=(1<<i);
                cout<<"? "<<a<<" "<<b<<endl;//相当于让a和b的第i位都和0异或,即再次判断a和b的值是否相等
                fflush(stdout);
                cin>>ff;
            }
            else    //(a^c) 和 (b^d) 的大小关系没有发生变化
            {
                cout<<"? "<<(a|(1<<i))<<" "<<b<<endl; //相当于让a的第i位和1异或,b的第i位和0异或  
                fflush(stdout);
                cin>>f;
                if(f==-1)//a和b的第i位都是1
                    {a|=(1<<i);b|=(1<<i);}
            }
        }
    }
    
    cout<<"! "<<a<<" "<<b<<endl;
    
    return 0;
}

参考博客:https://www.cnblogs.com/Dup4/p/10068891.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值