题目链接

D. Ehab and another another xor problem

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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⊕da⊕c>b⊕d.
  • 0 if a⊕c=b⊕da⊕c=b⊕d.
  • -1 if a⊕c<b⊕da⊕c<b⊕d.

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

Laggy should guess (a,b)(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<2300≤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).

Example

input

Copy

1
-1
0

output

Copy

? 2 1
? 1 2
? 2 0
! 3 1

Note

In the sample:

The hidden numbers are a=3a=3 and b=1b=1.

In the first query: 3⊕2=13⊕2=1 and 1⊕1=01⊕1=0, so the answer is 1.

In the second query: 3⊕1=23⊕1=2 and 1⊕2=31⊕2=3, so the answer is -1.

In the third query: 3⊕2=13⊕2=1 and 1⊕0=11⊕0=1, so the answer is 0.

Then, we printed the answer.

【题意】

你可以询问最多62个问题,每个问题描述为两个数c,d。系统会给出a xor c 与b xor d的大小关系,询问最多62个问题后输出a,b。

【解题思路】

参考博主:https://blog.csdn.net/qq_40791842/article/details/84836891

可以说写的很明白了。有些地方可能需要手动写一下方便更好的理解。

做这题的时候需要从高位向低位遍历,考虑到只有30位但有62次询问,可以简化成每一次询问先询问当前位到最低位a和b的大小,比较出大小之后再确定当前位是1还是0。

那么如何比较当前位到最低位之间a和b的大小呢?只要记a当前位到最高位的值为c,同理记b当前位到最高位的值为d,异或一下就行啦。比如a:10101,b:11001,假设从高到低位次依次为43210。假设本次比较的是第3位,那么c为10000,d为11000,将a异或c为00101,d异或b为00001,其实就是消去高位到当前位的影响啦。

然后就要确定当前位是1还是0啦。

①ask(prea,preb)=0

表示从当前位至最低位的所有位ab在该位都对应相等,那么只用判断每一位是0还是1就行了。可以ask(prea+(1<<i),preb)。

若为-1,该位是1;若为1,该位是0。

②ask(prea,preb)=1

表示当前位至最低位的二进制数a>b,设a的当前位为a[ i ],b为b[ i ],那么只会存在以下三种情况:
   (1) a[ i ]=1,b[ i ]=0;
   (2) a[ i ]=1,b[ i ]=1;
   (3) a[ i ]=0,b[ i ]=0;

那么接下来怎么判断呢?

可以ask(prea+(1<<i),preb+(1<<i));

<1> 如果等于-1, 那么一定有a[ i ]=1, b[ i ]=0;

<2> 如果等于1, 那么可以是第(2) (3)两种中的其中一种;继续ask(prea+(1<<i),preb);若为-1,则表明a[ i ]=b[ i ]=1,否则为0

③ask(prea,preb)=-1

和第2种情况类似!

但是要注意并不是遍历到每一位都要去ask(prea,preb),比如,如果确定当前位 a[ i ]=b[ i ]=1或者a[ i ]=b[ i ]=0,那么下一位ask(prea,preb)的值是和当前的值是一样的,所以循环到下一位的时候就不需要ask了,只有a[ i ] !=b[ i ]的时候下一位才需要重新ask。

【代码】

#include<bits/stdc++.h>
using namespace std;
int ask(int c,int d)
{int flag;printf("? %d %d\n",c,d);fflush(stdout);scanf("%d",&flag);return flag;
}
int main()
{int prea=0,preb=0;int flag=ask(prea,preb);for(int i=29;i>=0;i--){if(flag==0){int flag2=ask(prea+(1<<i),preb);if(flag2==-1){prea+=(1<<i);preb+=(1<<i);}}else if(flag==1){int flag2=ask(prea+(1<<i),preb+(1<<i));if(flag2==1){int flag3=ask(prea+(1<<i),preb);if(flag3==-1){prea+=(1<<i);preb+=(1<<i);}}else{prea+=(1<<i);flag=ask(prea,preb);}}else{int flag2=ask(prea+(1<<i),preb+(1<<i));if(flag2==-1){int flag3=ask(prea+(1<<i),preb);if(flag3==-1){prea+=(1<<i);preb+=(1<<i);}}else{preb+=(1<<i);flag=ask(prea,preb);}}}printf("! %d %d\n",prea,preb);fflush(stdout);
}

【Codeforces Round #525(Div. 2)】Ehab and another another xor problem(思维+异或)相关推荐

  1. Codeforces Round #731 (Div. 3) G. How Many Paths? dfs + 拓扑 + 思维

    传送门 题意: 给你一张nnn个点mmm条边的图,让你对每个点确定一个编号,规则如下: (1)(1)(1) 对于不能到的点编号为000. (2)(2)(2) 对于只有一条路径能到这个点的点编号为111 ...

  2. Codeforces Round #716 (Div. 2) D. Cut and Stick 主席树 + 思维

    传送门 文章目录 题意: 思路: 题意: 给你个长为nnn的数组aaa,定义好的区间为这个区间中每个数出现的次数≤⌈n2⌉\le \left \lceil \frac{n}{2} \right \rc ...

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

    题目:http://codeforces.com/contest/1088/problem/D 题目大意:有0 <= a , b <= 230的a,b两个整数,定义一次操作(c , d)的 ...

  4. Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip 二进制拆位+树型dp

    E. Mahmoud and a xor trip 链接: http://codeforces.com/contest/766/problem/E 题意: 给定一颗n节点的树以及每个节点的权值,另di ...

  5. Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)

    题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...

  6. Codeforces Round #697 (Div. 3) G. Strange Beauty 线性筛变形思维

    原题链接 题意: n个数,问删除多少个数之后剩下的数组中的数满足:对于任意一对i,j满足ai%aj==0 || aj%ai ==0. 思路: 题目给的ai只有2e5,很容易想到从ai入手,算出如果当前 ...

  7. szu cf集训Codeforces Round #631 (Div. 2)A ~ D[贪心,数据结构,思维,dp]

    A. Dreamoon and Ranking Collection 题意:题意不太好理解.简单来讲就是,给出一组数,能从1最多数到几,不够的用数来填,最多填x次.思路:代码很简单-先出现过的地方肯定 ...

  8. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...

  9. Codeforces Round #619 (Div. 2) F. Super Jaber 多源bfs + 思维转换

    传送门 文章目录 题意: 思路: 题意: 给你一个矩阵,每个格子都有一个颜色kkk,每秒可以移动到相邻矩阵或者瞬移到同一颜色的任意矩阵.有qqq个询问,每次询问给出两个点,求从一个点到另一个点的最短时 ...

最新文章

  1. ATS名词术语(待续)
  2. TCP四次握手释放连接
  3. 数论 欧几里得与扩展欧几里得
  4. SQLServer 优化SQL语句:in 和not in的替代方案
  5. java pdf转base64_后台返回pdf的base64编码到前端,如果pdf中有中文,不会显示问题?...
  6. jQuery Tips(5)----关于伪类选择符
  7. 第二十一天 认识一维数组part3
  8. C++实现简单的文件I/O操作
  9. LG WP7机型工程模式下越狱
  10. 超实用压力测试工具-ab工具
  11. flask中基础模板templates
  12. 在word表格中如何快速清除表格内容?
  13. 浅谈矢量场 —— 1. 梯度、散度与拉普拉斯算子
  14. 自建服务器同步软件,黑群晖自建anki服务器电脑anki软件设置同步
  15. android怎么调textview间距,Android如何设置TextView的行间距、行高。
  16. 中国矿业大学测绘工程专业课-学习笔记
  17. 与 SQL Server 2012 建立连接时出现与网络相关的或特定于实例的错误。
  18. windows安装.Net Framework3.5无法安装问题
  19. DX 纹理像素格式转换算法 R10G10B10A2 转 R8G8B8A8
  20. 电脑频繁死机,在进行CMOS设置时也会出现死机现象

热门文章

  1. 另类看设计模式[来自酷壳网]
  2. Reinforcement learning:an introduction-学习笔记-chapter1
  3. php企业微信发送微信群组消息,企业微信可以给群成员单独群发消息吗?怎么给企业微信群里所有成员群发消息?...
  4. 从URL到浏览器页面渲染完成
  5. sikuli java_Sikuli--基于像素的图像识别(JAVA)
  6. linux dd iflag oflag,Linux dd 命令
  7. 任性 CSS 实现 Donut loading
  8. 放弃几百万年薪的后续
  9. CSE RPC流程分析
  10. R语言读取txt文件中的内容