D. Ehab and another another xor problem
time limit per test 1 second
memory limit per test 256 megabytes
inputstandard input
outputstandard output
This is an interactive problem!
Ehab plays a game with Laggy. Ehab has 2 hidden integers (a,b). Laggy can ask a pair of integers (c,d) and Ehab will reply with:
if
if
if
Operation a⊕b is the bitwise-xor operation of two numbers a and b.
Laggy should guess (a,b) with at most 62 questions. You’ll play this game. You’re Laggy and the interactor is Ehab.
It’s guaranteed that .
Input
See the interaction section.
Output
To print the answer, print “! a b” (without quotes). Don’t forget to flush the output after printing the answer.
Interaction
To ask a question, print “? c d” (without quotes). Both c and d must be non-negative integers less than
. Don’t forget to flush the output after printing any question.
After each question, you should read the answer as mentioned in the legend. If the interactor replies with -2, that means you asked more than 62 queries and your program should terminate.
To flush the output, you can use:-
fflush(stdout) in C++.
System.out.flush() in Java.
stdout.flush() in Python.
flush(output) in Pascal.
See the documentation for other languages.
Hacking:
To hack someone, print the 2 space-separated integers a and b .
Example
inputCopy
1
-1
0
outputCopy
? 2 1
? 1 2
? 2 0
! 3 1
思路:很明显我们需要一位一位的来确定a,b。从高位向低位来确定会简单点,假设a,b的高前几位已经确定好,当前正在确定第k位。有四种情况:
然后询问
若
则
若
则
,此时要根据a,b低
位大小的比较才能确定;
若
则
此时要根据a,b低
位大小的比较才能确定;
若
则
所以当 时,若 ;否则
如何确定低 位的大小呢,我们可以一开始就比较出a,b的大小,然后逐步向下推。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6+10;
typedef long long ll;
int ask(ll x,ll y)
{
printf("? %lld %lld\n",x,y);
fflush(stdout);
int ans;
scanf("%d",&ans);
return ans;
}
int main()
{
ll a=0,b=0;
int tag=ask(0,0); //确定a,b大小
for(int i=29;i>=0;i--)
{
int L=ask(a^(1ll<<i),b);
int R=ask(a,b^(1ll<<i));
if(L==R)
{
if(tag==-1)b^=1ll<<i;//低i位 a<b
if(tag==1)a^=1ll<<i; //低i位 a>b
tag=L; //递推tag
}
else if(L==-1)
{
a^=1ll<<i;
b^=1ll<<i;
}
}
printf("! %lld %lld\n",a,b);
return 0;
}