Codeforces Round #781 (Div. 2) - D. GCD Guess

GCD + 位运算

[Problem - 1665D - Codeforces](https://codeforces.com/problemset/problem/1627/D)

题意

交互题,有一个未知数 x(1<=x<=109), 最多有 30 次询问,每次询问给出 1<=a,b<=109, 返回 gcd(a+x,b+x), 求出 x

思路

  1. 30 次询问,一开始想二分,但没找到单调性;
  2. 按位来判断,如果每次能判断 1 位也正好满足条件
  3. 如果已经求出了 x 的前 i - 1位,记为 r;对于第 i 位,gcd(xr,2i) == 2i 则 x 的第 i 位是 0
  4. 但询问是 x 加一个数,不能询问 xr;所以可以询问 gcd(xr+2i,2i+1)==2i+1, 则 x 的第 i 位是 1
  5. a=2ir,b=r+2i+2i+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;
}
posted @   hzy0227  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!
点击右上角即可分享
微信分享提示