Codeforces Round #781 (Div. 2) - D. GCD Guess
GCD + 位运算
[Problem - 1665D - Codeforces](https://codeforces.com/problemset/problem/1627/D)
题意
交互题,有一个未知数 , 最多有 30 次询问,每次询问给出 , 返回 , 求出 x
思路
- 30 次询问,一开始想二分,但没找到单调性;
- 按位来判断,如果每次能判断 1 位也正好满足条件
- 如果已经求出了 x 的前 i - 1位,记为 r;对于第 i 位, == 则 x 的第 位是 0
- 但询问是 x 加一个数,不能询问 ;所以可以询问 , 则 x 的第 i 位是 1
- 令 即可
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
int y;
void query(int a, int b)
{
cout << "? " << a << " " << b << endl;
cout.flush();
cin >> y;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
int r = 0;
for (int i = 0; i < 30; i++)
{
int a = (1 << i) - r;
int b = a + (1 << i + 1);
query(a, b);
if (y == (1 << i + 1))
r |= (1 << i);
}
cout << "! " << r << endl;
cout.flush();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!