Codeforces Round #525 D - Ehab and another another xor problem /// 构造
题目大意:
本题有两个隐藏起来的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
1
-1
0
? 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 /// 构造的更多相关文章
- Codeforces Round #525 E - Ehab and a component choosing problem
题目大意: 在一棵树中 选出k个联通块 使得 这k个联通块的点权总和 / k 最大 并且这k个联通块不相互覆盖(即一个点只能属于一个联通块) 如果有多种方案,找到k最大的那种 给定n 有n个点 给定n ...
- 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的 ...
- 【CF1174D】 Ehab and the Expected XOR Problem - 构造
题面 Given two integers \(n\) and \(x\), construct an array that satisfies the following conditions: · ...
- 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)
Codeforces Round #525 (Div. 2) 哎,忍不住想吐槽一下,又要准备训练,又要做些无聊的事,弄得我都想退出了. 好好的训练不好么???? 只能做出两道水题,其实C题,感觉做出来 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #525 (Div. 2) Solution
A. Ehab and another construction problem Water. #include <bits/stdc++.h> using namespace std; ...
- CodeForces Round 525
A:Ehab and another construction problem #include<bits/stdc++.h> using namespace std; #define F ...
- Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题
Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] 总共两次询 ...
随机推荐
- codeforces 447E or 446C 线段树 + fib性质或二次剩余性质
CF446C题意: 给你一个数列\(a_i\),有两种操作:区间求和:\(\sum_{i=l}^{r}(a[i]+=fib[i-l+1])\).\(fib\)是斐波那契数列. 思路 (一) codef ...
- (2)centos7 图形界面
1.登录 2.先安装MATE可视化桌面 yum groups install "MATE Desktop" 选择y 3.安装X Window System:图形接口 yum gro ...
- git分布式版本控制系统权威指南学习笔记(四):git reset
文章目录 git reset目录树重写 git reset 重置 git reset目录树重写 git reset --soft 暂存区工作区不变 git reset --hard git reset ...
- 5、如何快速找到多个字典中的公共键(key) 6 如何让字典保持有序 7 如何实现用户的历史记录功能(最多n条)
5.如何快速找到多个字典中的公共键(key) from random import randint,sample #随机取数 # a = sample("ABCDEF",randi ...
- Size Assert
判断返回内容的大小
- (Struts2学习系列四)Struts2指定配置文件
我们的每个action都在struts.xml里配置的话,就会出现很多的xml语句,单单一个struts.xml就会变得很大,所以我们会在struts.xml里使用include引入其他的.xml文件 ...
- zic - 时区编辑器
总览 zic [ -v ] [ -d directory ] [ -l localtime ] [ -p posixrules ] [ -L leapsecondfilename ] [ -s ] [ ...
- 机器学习Explainability vs Interpretability
The difference between machine learning explainability and interpretability In the context of machin ...
- kubeadm生成的token重新获取
当你的token忘了或者过期,解决办法如下: 1.先获取token #如果过期可先执行此命令kubeadm token create #重新生成token#列出tokenkubeadm token l ...
- springmvc之json交互406异常(Not Acceptable)和415异常(Unsupported Media Type)
一. 406异常(Not Acceptable) 1. 没有添加jackson-databind包2. 请求的url的后缀是*.html.在springmvc中如果请求的后缀是*.html的话,是不可 ...