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 个数全都加上 ...
随机推荐
- admin添加用户时报错:(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_l
在stackoverflow找到答案: DATABASES = { 'default': { ... 'OPTIONS': { "init_command": "SET ...
- 第1章 MATLAB概述
MATLAB系统由~开发环境.~语言.~数学函数库.~图形处理系统.~应用程序接口(API)5大部分组成. 界面 命令行中的语句格式 命令行的语句格式:>>变量=表达式(没有>> ...
- (数据科学学习手札23)决策树分类原理详解&Python与R实现
作为机器学习中可解释性非常好的一种算法,决策树(Decision Tree)是在已知各种情况发生概率的基础上,通过构成决策树来求取净现值的期望值大于等于零的概率,评价项目风险,判断其可行性的决策分析方 ...
- MyEclipse 上使用sping+hibernate+mysql
以下为入门级别代码,高手请务见笑~ 我的MyEclipse 上spring最高版是3.* 而hibernate 最高版本是4.* 在做项目时用的都是最高版,于是代码写好之后调试报了个异常,居然 ...
- 为什么我要放弃javaScript数据结构与算法(第一章)—— JavaScript简介
数据结构与算法一直是我算比较薄弱的地方,希望通过阅读<javaScript数据结构与算法>可以有所改变,我相信接下来的记录不单单对于我自己有帮助,也可以帮助到一些这方面的小白,接下来让我们 ...
- 如何将h5网页改成微信网页
1.如何将h5网页改成微信网页 1.设置安全域名 先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. 备注:登录后可在“开发者中心”查看对 ...
- C++11中std::move的使用
std::move is used to indicate that an object t may be "moved from", i.e. allowing the effi ...
- Java中的IO流体系
Java为我们提供了多种多样的IO流,我们可以根据不同的功能及性能要求挑选合适的IO流,如图10-7所示,为Java中IO流类的体系. 注:这里只列出常用的类,详情可以参考JDK API文档.粗体标注 ...
- java二分法来求一个数组中一个值的key
package TestArray; import java.util.Arrays; /** * 二分法查找 */ public class Test { public static void ma ...
- CC3200模块的内存地址划分和bootloader(一)
1. CC3200的内存地址划分非常特殊,如果没测试的话,很容易懵逼.我们先看芯片手册里面的内存地址.芯片的RAM是256KB,下图的0x2000 0000-0x2003 FFFF,正好是256KB. ...