题目大意:

本题有两个隐藏起来的a b(1<=a,b<=1e30)

每次可 printf("? %d %d\n",c,d); 表示询问 a^c 与 b^d 的相对大小 最多可询问62次

然后 scanf("%d",&ans); 读入返回的答案

ans=1 说明 a^c > b^d

ans=0 说明 a^c = b^d

ans=-1 说明 a^c < b^d

当通过询问确定了a b就 printf("! %d %d\n",a,b); 表示得到答案为 a b

input

Copy
1
-1
0
output

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

询问 a^2 与 b^1 得到 1

询问 a^1 与 b^2 得到 -1

询问 a^2 与 b^0 得到 0

就可以确定隐藏的 a=3 b=1

代码里有详细注释

这里我们举个简单的栗子

一开始 a^0比较b^0 可得到a b的相对大小 此时假设a>b

a>b时则a b第i位有三种可能

取反最高位后再判断a b的相对大小

a   b

1   1   (a b都取反)-> 0  0   (再比较a b可得到a>b) -> 无法确定(1式)

1   0   (a b都取反)-> 0  1   (再比较a b可得到a<b) -> 可说明ai=1 bi=0

0   0   (a b都取反)-> 1  1   (再比较a b可得到a>b) -> 无法确定(2式)

(1式) (2式) 无法确定 继续询问

a   b

1   1   (a取反)-> 0  1   (再比较a b可得到a<b) -> 可说明ai=1 bi=1

0   0   (a取反)-> 1  0   (再比较a b可得到a>b) -> 可说明ai=0 bi=0

a=1101 b=1011(a>b)

取反a b第一位 a=0101 b=0011 询问可得a>b 无法判断

此时只取反a的第一位 a=0101 b=1011 询问可得a<b 说明a的第一位为1 b的第一位为1

第一位已确定 去掉第一位a=101 b=011(a>b)

那么取反此时a b第二位 a=001 b=111 询问可得a<b 说明a的第二位为1 b的第二位为0

第二位已确定 去掉第二位a=01 b=11(a<b)

那么取反此时a b第三位 a=11 b=01 询问可得a>b 说明a的第三位为0 b的第三位为1

第三位 已确定 去掉第三位 a=1 b=1(a<b)

那么取反此时a b第四位 a=0 b=0 询问可得a=b 无法判断

此时只取反a的第四位 a=0 b=1 询问可得a<b 说明a的第四位为1 b的第四位为1

最终得到a b

#include <bits/stdc++.h>
using namespace std;
int a, b;
int maybe(int c,int d) {
printf("? %d %d\n",c,d);
fflush(stdout);
int ans; scanf("%d",&ans);
return ans;
}
int main()
{
int f=,la=,lb=;
for(int i=;i>=;i--) {
// la lb为a b二进制第i位往左 包含第i位为lai lbi
// ra rb为a b二进制第i位往右 包含第i为为rai rbi
// ai bi为a b二进制第i位
if(f==) { // f=0时去判断rai rbi的相对大小
if(maybe(la,lb)==-) f=-; // rai<rbi
else f=; // rai>=rbi
}
if(f==) {
/* 已知 rai >= rbi
则a b第i位有三种可能
ai bi (将ai bi取反 la^(1<<i) lb^(1<<i))
1 1 -> 0 0 -> maybe(la,lb)=1或0 (1式)
1 0 -> 0 1 -> maybe(la,lb)=-1即rai<=rbi -> 可得到ai=1 bi=0 此时ra rb的相对大小则不确定了
0 0 -> 1 1 -> maybe(la,lb)=1或0 (2式)
(1式)=(2式) 无法确定
ai bi (只将ai取反 la^(1<<i) )
1 1 -> 0 1 -> maybe(la,lb)=-1即rai<=rbi -> 可得到ai=1 bi=1 此时ra rb的相对大小仍不变
0 0 -> 1 0 -> maybe(la,lb)=1或0即rai>=rbi-> 可得到ai=0 bi=0 此时ra rb的相对大小仍不变
将得到的结果更新到la lb 然后i继续往右判断 */
if(maybe(la^(<<i),lb^(<<i))==-)
la^=(<<i), f=; // 下轮i往右 需要判断rai rbi的相对大小
else if(maybe(la^(<<i),lb)==-)
la^=(<<i), lb^=(<<i);
} else {
/* f=-1同理f=1 相反过来判断 */
if(maybe(la^(<<i),lb^(<<i))!=-)
lb^=(<<i), f=; // 下轮i往右 需要判断rai rbi的相对大小
else if(maybe(la,lb^(<<i))==)
la^=(<<i), lb^=(<<i);
}
}
printf("! %d %d\n",la,lb);
fflush(stdout); return ;
}

Codeforces Round #525 D - Ehab and another another xor problem /// 构造的更多相关文章

  1. Codeforces Round #525 E - Ehab and a component choosing problem

    题目大意: 在一棵树中 选出k个联通块 使得 这k个联通块的点权总和 / k 最大 并且这k个联通块不相互覆盖(即一个点只能属于一个联通块) 如果有多种方案,找到k最大的那种 给定n 有n个点 给定n ...

  2. cf1088D Ehab and another another xor problem (构造)

    题意:有两数a,b,每次你可以给定c,d询问a xor c和b xor d的大小关系,最多询问62次($a,b<=2^{30}$),问a和b 考虑从高位往低位做,正在做第i位,已经知道了a和b的 ...

  3. 【CF1174D】 Ehab and the Expected XOR Problem - 构造

    题面 Given two integers \(n\) and \(x\), construct an array that satisfies the following conditions: · ...

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

    D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...

  5. Codeforces Round #525 (Div. 2)

    Codeforces Round #525 (Div. 2) 哎,忍不住想吐槽一下,又要准备训练,又要做些无聊的事,弄得我都想退出了. 好好的训练不好么???? 只能做出两道水题,其实C题,感觉做出来 ...

  6. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  7. Codeforces Round #525 (Div. 2) Solution

    A. Ehab and another construction problem Water. #include <bits/stdc++.h> using namespace std; ...

  8. CodeForces Round 525

    A:Ehab and another construction problem #include<bits/stdc++.h> using namespace std; #define F ...

  9. Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题

    Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] ​ 总共两次询 ...

随机推荐

  1. jmter 二次开发 IDEA 项目5.1

    jmter 二次开发 IDEA 项目5.1 IDEA 编译 Jmeter 5.0(二次开发) 1. Java环境配置 1.1     步骤1 1.2     步骤2 1.3     步骤3 1.4   ...

  2. git clone慢的解决办法

    转自:http://www.kindemh.cn/ 转自Kindem的博客 问题 大家可能都遇到过从github使用git clone指令奇慢无比的问题,网上很多人说使用代理来加速git,但是这也不是 ...

  3. adis16405 配置

  4. 牛客 Fruit Ninja 2018 ACM 上海大都会赛 (随机化算法)

    题目链接:Fruit Ninja 比赛链接:2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 题目描述 Fruit Ninja is a juicy action game enjoyed ...

  5. Java各版本的含义

    JavaSE(Java Standard Edition):标准版,定位在个人计算机上的应用.这个版本是Java平台的核心,它提供了非常丰富的API来开发一般个人计算机上的应用程序,包括用户界面接口A ...

  6. mysql shell脚本

    mysql shell连接脚本 本地连接及远程链接 #!/bin/bash #连接MySQL数据库 Host=127.0.0.1 User=username PASSWORD=password POR ...

  7. js关于if()else{}中的判定条件的认识,各种数据类型转换为Boolean类型的转换规则

    博客搬迁,给你带来的不便敬请谅解! http://www.suanliutudousi.com/2017/09/24/js%E5%85%B3%E4%BA%8Eifelse%E4%B8%AD%E7%9A ...

  8. vue组件的inheritAttrs属性

    vue官网对于inheritAttrs的属性解释:如果你不希望组件的根元素继承特性,你可以在组件的选项中设置 inheritAttrs: false. 可能不是很好理解,我们可以举个例子来验证一下. ...

  9. 连接分析算法-HITS-算法

    转自http://blog.csdn.net/Androidlushangderen/article/details/43311943 参考资料:http://blog.csdn.net/hguisu ...

  10. form编码方式application/x-www-form-urlencoded和multipart/form-data的区别?

    application/x-www-form-urlencoded: 表单数据编码为键值对,&分隔 multipart/form-data: 表单数据编码为一条消息,每个控件对应消息的一部分 ...