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
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.
See the interaction section.
To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer.
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 ;
}
Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem的更多相关文章
- Codeforces Round #525 (Div. 2) D. Ehab and another another xor problem(待完成)
参考资料: [1]:https://blog.csdn.net/weixin_43790474/article/details/84815383 [2]:http://www.cnblogs.com/ ...
- Codeforces Round #525 (Div. 2) D. Ehab and another another xor problem(交互题 异或)
题目 题意: 0≤a,b<2^30, 最多猜62次. 交互题,题目设定好a,b的值,要你去猜.要你通过输入 c d : 如果 a^c < b^d ,会反馈 -1 : 如果 a^c = b^ ...
- Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...
- Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem 数学
题意:给出树 求最大的sigma(a)/k k是选取的联通快个数 联通快不相交 思路: 这题和1个序列求最大的连续a 的平均值 这里先要满足最大平均值 而首先要满足最大 也就是一个数的时候可 ...
- Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula
F. Ehab and a weird weight formula 题目链接:https://codeforces.com/contest/1088/problem/F 题意: 给出一颗点有权值的树 ...
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...
- Codeforces Round #525 (Div. 2)A. Ehab and another construction problem
A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...
- Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task 数学 mod运算的性质
C. Ehab and a 2-operation task 数学 mod运算的性质 题意: 有两种对前缀的运算 1.对前缀每一个\(a +x\) 2.对前缀每一个\(a\mod(x)\) 其中x任选 ...
- Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task
传送门 https://www.cnblogs.com/violet-acmer/p/10068786.html 题意: 给定一个长度为 n 的数组a[ ],并且有两种操作: ①将前 i 个数全都加上 ...
随机推荐
- Unity 对象的批处理
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52813834 作者:car ...
- nginx proxy_cache缓存详解
目录 1. 关于缓冲区指令 1.1 proxy_buffer_size 1.2 proxy_buffering 1.3 proxy_buffers 1.4 proxy_busy_buffers_siz ...
- 使用LinqToExcel和EPPlus操作excel
1.使用LinqToExcel LinqToExcel是一个.net framework平台下开源项目,它主要实现了LINQ的语法查询Excel电子表格.类型之前的LINQToXXX如果你是LINQ语 ...
- android中接入twitter进行第三方登录
在应用中接入Twitter进行第三方登录时,开发人员遇到了一点问题,主要是概念有点混乱,这里把经验记录一下,帮助遇到同样问题的朋友. 一.注册应用并配置登录权限 这一步比较简单,就不多说了,直接去官网 ...
- Win10开发笔记(一):一些VS2015中可能遇到的问题
Win10开发者交流群:53078485 一.VS2015部署Win10程序到手机出现“0x80073CFD”错误解决方案 在VS2015 RC中创建了Windows Universal程序,部署到手 ...
- 线段树简单入门 (含普通线段树, zkw线段树, 主席树)
线段树简单入门 递归版线段树 线段树的定义 线段树, 顾名思义, 就是每个节点表示一个区间. 线段树通常维护一些区间的值, 例如区间和. 比如, 上图 \([2, 5]\) 区间的和, 为以下区间的和 ...
- C#异步了解一下
如何让你的代码在“同一时间”干着两件件事呢?比如说,在初始化加载配置的同时,UI界面能够响应用户的各种点击事件.而不置于卡死,特别是出现如下面这种情况的时候,对于用户来说是很崩溃的.
- MySQL☞order by与distinct
asc(升序,默认值)/desc(降序) 1.根据某一列的列值进行升序或者降序操作. select 列名 别名 from 表名 order by 列名 asc/desc 2.根据多个列值进行排序 s ...
- 7.0 启动app权限弹窗问题
这里提供两种解决方案! 1.安卓6.0+是可以直接利用uiautomator定位元素点击!这个不细说,定位方式很多种...这个等待时间大家自己定大概两到三秒即可! #安卓6.0+点击方式driver. ...
- linux下 su 与 su - 的区别和使用
Linux下su与su -命令的区别 在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用servic ...