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. ES之分析器(Analyzer)及拼音分词器

    把输入的文本块按照一定的策略进行分解,并建立倒排索引.在Lucene的架构中,这个过程由分析器(analyzer)完成. 主要组成 character filter:接收原字符流,通过添加.删除或者替 ...

  2. lbaas

    目录 - LBaaS v2 - 负载均衡概念 - 服务器池 Pool - 监听器 Listener - L7 转发策略 l7 policy - 负载均衡算法 Algorithms - 健康监测 Mon ...

  3. 如何使用源码编译安装Nginx服务器

    安装 PCRE : 网站:http://pcre.org/ 下载: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ ftp://ftp.c ...

  4. yum源更换为阿里云源

    #首先备份/etc/yum.repos.d/CentOS-Base.repocp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...

  5. kubeSphere+kubernetes 集群更新证书

    模拟问题点 使用kubernetes时错误提示 yang@master:~$ kubectl get nodes Unable to connect to the server: x509: cert ...

  6. vue组件自调用

    组件自调用 在vue中有些情况下我们会用到组件本身来遍历出自己想要的效果. 其实非常简单,可以在模板中使用name属性值,来调用自己 <template> <el-submenu v ...

  7. python调用golang代码

    一.调用步骤: 将go代码编译成so库 -> python中通过ctypes引用so库并指定需要调用的函数(同时可指定传入参数类型和返回值类型) -> 指定后按python使用函数方式调用 ...

  8. 微服务笔记之Euraka(2)

    Eureka Server启动过程 入口:SpringCloud充分利用了SpringBoot的自动装配的特点 Eureka-server的jar包,发现在MERA-INF下面有配置文件spring. ...

  9. gcc_to_use

    gcc 目录 gcc 概要 基本指令及功能(以gcc为例) gcc -gdb gcc -cmake 概要 GCC:GNU Compiler Collection(GUN 编译器集合),是GNU项目中符 ...

  10. 使用Mybatis plus xml 记录过程

    <select id="selectByConditions" resultType="com.springboot.domain.DemoQueryModel&q ...