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^d ,会反馈 0 ;
如果 a^c > b^d ,会反馈 1 ;
每次猜前面都用 ? 表示, 最后一行用!表示已经知道a b的值。
思路:
不会,然后去找别人博客学。 大致思路就是:a b都是二进制来表示,从高到低位 把a和b的每一位都判断出来。
判断a b同一位是否相等:
1. 如果相等,判断这一位是0 还是 1 ,
判断方法是:printf("? %d %d\n", a|(1<<i),b);
scanf("%d",&now); now是反馈
如果now是1,则a b 的这一位都是1(a=a|(1<<i), b=b|(1<<i) )
2. 如果不等,判断 a这一位是1 && b这一位是0 还是 a这一位是0 && b这一位是1 ,
判断方法是:见代码
步骤:
一开始通过c=0 d=0 判断a b的相对大小:
1. 如果a==b,说明a和b的所有二进制位都是相同的,按上面的1进行解决;
2. 如果a !=b,说明a b存在某些二进制位不相同的情况,所以:
首先判断当前位是否相等,如果相等,按上面1的做法 ; 如果不相等,按上面2的做法。
再判断 当前位以后的所有位 是否全都相同,如果相同,进入此1,如果不相同,进入此2.
#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const ll mod=1e9+;
const int INF= 0x3f3f3f3f;
const int N=1e5; int main()
{
int a=,b=,fb,now;
printf("? 0 0\n");
fflush(stdout);
scanf("%d",&fb); for(int i=;i>=;i--)
{
if(fb==)//a b 在当前i位以后的 所有都相同
{
printf("? %d %d\n",a|(<<i),b);
fflush(stdout);
scanf("%d",&now); //判断当前这位是1还是0
if(now==-) //是1
a=a|(<<i), b=b|(<<i);
}
else//a b 在当前i位以后的各位 存在不同
{
printf("? %d %d\n",a|(<<i),b|(<<i));//判断当前位是否相同
fflush(stdout);
scanf("%d",&now);
if(now==fb) //这一位的改变不影响ab之间谁大谁小,所以是相同的
{
printf("? %d %d\n",a|(<<i),b);
fflush(stdout);
scanf("%d",&now); //判断这位是1还是0
if(now==-) //是1
a=a|(<<i), b=b|(<<i);
}
else // a中的这一位和b中的这一位 不相同
{
//假设答案a为101,b为001,代码中a=0,b=0,
//所以a|(1<<2)=100,b|(1<<2)=100, 101^100=001,001^100=101,
//此时now==-1,说明a这位为1,b这位为0; //如果答案b为101,a为001,代码中a=0,b=0,
//所以a|(1<<2)=100,b|(1<<2)=100,001^100=101,101^100=001,
//此时now==1,说明a这位为0,b这位为1;
if (now==-) a |= << i;
else if(now==) b |= << i; //因为now不可能=0了 printf("? %d %d\n",a,b);
//这一位判断完毕,接着算a b在这位以后所有的是不是相同
fflush(stdout);
scanf("%d",&fb);
}
}
}
printf("! %d %d\n", a, b);
fflush(stdout);
}
Codeforces Round #525 (Div. 2) D. Ehab and another another xor problem(交互题 异或)的更多相关文章
- 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 ...
- 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)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 个数全都加上 ...
随机推荐
- mysql数据库建表授权操作
1.create schema [数据库名称] default character set utf8 collate utf8_general_ci;--创建数据库 采用create schema和c ...
- 常见问题:计算机网络/运输层/UDP
几乎不对IP增加其他东西,无连接. 优势 速度快.适合实时. 无连接建立,没有连接时延. 无连接状态. 分组首部开销小.TCP需20字节,UDP仅需8字节. 使用UDP的协议 DNS SNMP RIP ...
- didSelectRowAtIndexPath方法无响应解决办法
tableview的代理事件didSelectRowAtIndexPath失效,可能有三个原因: 1.没有设置tableview的Delegate 需要设置tableview的代理才可以响应代理事件 ...
- php有关类和对象的相关知识1
有关类和对象的相关知识 类的自动加载 类的自动加载是指,在外面的页面中,并不需要去“引入”(包含)类文件,但是程序会在需要一个类的时候就自动去“动态加载”该类. 什么叫做“需要一个类”?通常是这样的情 ...
- Hive行列转换
Hive行列转换 1.行转列 (根据主键,进行多行合并一列) 使用函数:concat_ws(‘,’,collect_set(column)) collect_list 不去重 collect_s ...
- spring boot datasource 参数设置
datasource spring.dao.exceptiontranslation.enabled是否开启PersistenceExceptionTranslationPostProcessor,默 ...
- redis 主从 哨兵
数据库为什么要读写分离 写代码好多年了,大家先抛弃在代码框架里面各种花哨的设计之外,写的代码到最后无非就是为了增删查改数据库.一般项目数据库刚开始只是但一个库,随着数据量的增大,就开始优化数据库(抛开 ...
- 《Mysql - 在Mysql服务出现瓶颈时,有哪些“饮鸩止渴”提高性能的方法?》
一:情景 - 业务高峰期,生产环境的 MySQL 压力太大,没法正常响应,需要短期内.临时性地提升一些性能. - 在业务高发时候,Mysql 服务压力过大,导致业务受损, 用户的开发负责人说,不管你用 ...
- 随记sqlserver学习笔记(一)
create database testuse test --部门表create table department( dept_id int not null identity primary key ...
- 使用scrapy框架做赶集网爬虫
使用scrapy框架做赶集网爬虫 一.安装 首先scrapy的安装之前需要安装这个模块:wheel.lxml.Twisted.pywin32,最后在安装scrapy pip install wheel ...