GCD + 位运算

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

题意

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

思路

  1. 30 次询问,一开始想二分,但没找到单调性;
  2. 按位来判断,如果每次能判断 1 位也正好满足条件
  3. 如果已经求出了 x 的前 i - 1位,记为 r;对于第 i 位,\(\gcd(x-r,2^i)\) == \(2^i\) 则 x 的第 \(i\) 位是 0
  4. 但询问是 x 加一个数,不能询问 \(x-r\);所以可以询问 \(\gcd(x-r+2^i,2^{i+1})==2^{i+1}\), 则 x 的第 i 位是 1
  5. 令 \(a=2^i-r,\;b=-r+2^i+2^{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;
}

最新文章

  1. ios 弹出不同的键盘
  2. 【转】【51CTO 网+】怎样做一款让用户来电的产品
  3. coderforces 721b
  4. PHP中对淘宝URL中ID提取
  5. RCP:给GEF编辑器添加网格和标尺。
  6. .Net在线付款---Paypal在线付款开发过程
  7. SQL中关于日期的常用方法
  8. class training
  9. pycharm 注册
  10. checkbox绿色圆圈样式
  11. iOS8中的UIAlertController
  12. 仿酷狗音乐播放器开发日志二十五 duilib右键事件的不足的bug修复
  13. C#2.0至4.0 的一些特性
  14. 《Programming WPF》翻译 第8章 6.我们进行到哪里了?
  15. Android StrictMode介绍
  16. tee 解决readonly 文件无法修改
  17. Android APK反编译 apktool使用教程
  18. python文本读写数据
  19. HttpLogBrowser-GUI日志分析工具
  20. POJ2480 Longge&#39;s problem

热门文章

  1. 【Linux命令】在Linux服务器上与windows通过SCP命令互传文件时出现的问题排查过程
  2. docker 部署rocketmq 4.4
  3. Unity 打包到XCode自动化设置参数
  4. Vue中的样式作用域
  5. QT-groupBox组件内的组件失去交互功能
  6. Java语言打印空心菱形
  7. ssh登录、scp传送文件
  8. linux中用crontab定时任务启动jar无效
  9. 使用layui+jQuery实现点击数据修改,即点即改。
  10. ES-增删改查