Ehab and another another xor problem CodeForces - 1088D
阿新 • • 發佈:2018-12-12
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; }