D. Ehab and another another xor problem

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

Description:

This is an interactive problem!

Ehab plays a game with Laggy. Ehab has 2 hidden integers (a,b)(a,b). Laggy can ask a pair of integers (c,d)(c,d) and Ehab will reply with:

  • 1 if a⊕c>b⊕d.
  • 0 if a⊕c=b⊕d.
  • -1 if a⊕c<b⊕d.

Operation a⊕b is the bitwise-xor operation of two numbers aa and bb.

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 0≤a,b<230.

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 cc and dd must be non-negative integers less than 230230. 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 aa and bb (0≤a,b<230)(0≤a,b<230).

题意:

交互式题目。有两个隐藏的数a,b,每次可以给出询问"? c d",然后返回:

1.若a^c>b^d,返回1;

2.若a^c=b^d,返回0;

3.若a^c<b^d,返回-1。

然后最多进行62次操作,最终问a,b是多少?

题解:

我的第一道交互式题目~这题还是挺有意思的。

a,b最多小于2^30,并且操作次数最多62次,我们可以想到一位一位来分析。

然后模拟一下:

当a,b二进制中目前位置都为1时,分别异或1,0和0,1,那么肯定返回的是-1,1;

当a,b二进制中目前位置都为0时,分别异或1,0和0,1,返回的是1,-1;

当a,b二进制中目前位置不同时,异或1,0和0,1的结果是相同的,但正负并不一定。

我们现在主要考虑二进制中目前位置不同的情况,这种较为复杂,假设我们已知a>b,那么最高位不同时,肯定a的最高位是1,b的相同位置是0。

那么问题转化为了如何找a,b大小。

我们知道一开始的时候都异或0,0可以轻松地比较出a,b的大小,在之后第一次碰到不相同的情况(询问结果返回的值相同),可以确定;如果还能碰到第二次,那么就可以根据上一次的询问结果来判断大小(在第一次中异或后a,b的最高位变为相同的了,它会返回一个比较值,这个比较值就是后面二进制位数相比较的大小)。

每次找最高位,把当前位置之前的给异或掉就行了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; int query(int a,int b){
printf("? %d %d\n",a,b);
fflush(stdout);
int tmp;
scanf("%d",&tmp);
return tmp;
} int main(){
int a=,b=;
int big = query(a,b);
for(int i=;i>=;i--){
int x=query(a^(<<i),b),y=query(a,b^(<<i));
if(x==y){
if(big>) a^=(<<i);
else b^=(<<i);
big=(x==);
}else{
if(x==-) a^=(<<i),b^=(<<i);
}
}
printf("! %d %d",a,b);
return ;
}

最新文章

  1. lable计算行高
  2. T-SQL 比较N个指段取其中最大值
  3. 深入webx框架(li)
  4. Google Cloud Platform
  5. Java反序列化测试
  6. html tr td colspan
  7. 前端学习——css实用技术
  8. SQL优化之索引
  9. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
  10. Python-Django-Ajax
  11. 爬虫:输入网页之后爬取当前页面的图片和背景图片,最后打包成exe
  12. 爬虫之Resquests模块的使用(二)
  13. mysql基础----&gt;mybatis的批量插入(一)
  14. 京东某商品页面的简单爬取 --Pyhon网络爬虫与信息获取
  15. [LeetCode&amp;Python] Problem 530. Minimum Absolute Difference in BST
  16. http网站上传文件大小问题【没测试过】
  17. NCBI之gene系列
  18. select2插件替换掉数据列表为空时候的No results found的提示
  19. shell case语法
  20. tree -L n

热门文章

  1. 网站标题被篡改成北京赛车、PK10的解决处理办法
  2. ULINE(插入水平线)
  3. Java——equals方法---18.10.18
  4. 如何在WIN7_64环境下安装Oracle10g_64位版本
  5. 初步学习pg_control文件之三
  6. Hbase表格设计
  7. 【PHP】进一法取整、四舍五入取整、忽略小数等的取整数方法大全
  8. 手把手教你写css3通用动画
  9. python统计日志小脚本
  10. PAT——乙级1006:换个格式输出整数&amp;乙级1021:个位数统计&amp;乙级1031:查验身份证