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;
}

Codeforces Round #781 (Div. 2) - D. GCD Guess的更多相关文章

  1. Codeforces Round #323 (Div. 2) C. GCD Table 暴力

    C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...

  2. Codeforces Round #323 (Div. 2) C. GCD Table map

    题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...

  3. Codeforces Round #651 (Div. 2) B. GCD Compression(数论)

    题目链接:https://codeforces.com/contest/1370/problem/B 题意 给出 $2n$ 个数,选出 $2n - 2$ 个数,使得它们的 $gcd > 1$ . ...

  4. Codeforces Round #767 (Div. 2)——B. GCD Arrays

    B. GCD Arrays 题源:https://codeforces.com/contest/1629/problem/B 题目大意 给出一段区间[l, r],可以进行操作(把任意两个数拿出来,把他 ...

  5. Codeforces Round #323 (Div. 2) C.GCD Table

    C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is define ...

  6. Codeforces Round #323 (Div. 1) A. GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)

    对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i] ...

  8. Codeforces Round #651 (Div. 2) B. GCD Compression (构造)

    题意:有一个长度为\(2n\)的数组,删去两个元素,用剩下的元素每两两相加构造一个新数组,使得新数组所有元素的\(gcd\ne 1\).输出相加时两个数在原数组的位置. 题解:我们按照新数组所有元素均 ...

  9. Codeforces Round #838 (Div. 2) D. GCD Queries

    题意 有个长度为n的排列p,[0,1,2,...n-1],你可以进行至多2*n次询问,每次询问两个i,j,返回gcd(pi,pj),让你在规定时间内猜出0在哪两个位置之一 思路 这是一道交互题,询问的 ...

  10. Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和

    Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description ...

随机推荐

  1. c++练习271题:水仙花数

    *271题 原题传送门:http://oj.tfls.net/p/271 题解: #include<bits/stdc++.h>using namespace std; int cf(in ...

  2. windows mongo 开启副本集 6.x版本 mongo : 无法将“mongo”项识别为 cmdlet、函数、脚

    mongo报错 当前使用版本6.0.3,bin目录下并没有mongo.exe,所以没有mongo命令, 需要下载 https://www.mongodb.com/try/download/shell  ...

  3. JavaSE 日期时间类整理

    一.创建日期对象 1.创建日期对象 //1.直接创建日期 Date date1 = new Date(); //2.创建指定日期 使用Date类 目标 2000-5-10 Date date2 = n ...

  4. linux 中EOF用法

    EOF是END Of File的缩写,表示自定义终止符.既然自定义,那么EOF就不是固定的,可以随意设置别名,在linux按ctrl-d就代表EOF.EOF一般会配合cat能够多行文本输出.其用法如下 ...

  5. nginx的优化及防盗链

    简介: Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为"engine X",是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/S ...

  6. Word发博客测试

    这是一篇来自word的文章测试,测试word写博客发布效果. 操作参考:http://www.cnblogs.com/liezhengli/p/5854470.html 关键在于找到博客园的MetaW ...

  7. 02.java基础(一)java的基础、方法和数组

    目录 Java基础 Java特性 Java程序运行机制 Java基础语法 1.数据类型 基本类型 引用类型 数据类型扩展 String类型内存分配过程 转义字符 类型转换 变量 常量 2.运算符 逻辑 ...

  8. HCIP-进阶实验03-网络流量路径控制

    HCIP-进阶实验03-网络流量路径控制 实验需求 某城域网网络环境部署规划如图所示,该网络通过OSPF协议进行部署设计,分为四个区域,分别为骨干区域0.普通区域1.2.3.其中普通区域1为特殊区域N ...

  9. [iOS] iPhone,开发工具的一些杂项

    1.在safari的开发菜单里一直不显示我当前的iPhone,后来机缘巧合在 设置- 开发者 - Clear Trusted Computers ,重新信任电脑之后,就OK了(️)

  10. pyspark 中的rdd api 编码练习

    1,使用pyspark 的rdd api 进行了数据文件的处理,包括构建RDD, 统计分析RDD ,从文件中读取数据RDD,从文件中构建 rdd的模式shema. 然后通过模式,从rdd中生成data ...