Codeforces Round #781 (Div. 2) - D. GCD Guess
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
思路
- 30 次询问,一开始想二分,但没找到单调性;
- 按位来判断,如果每次能判断 1 位也正好满足条件
- 如果已经求出了 x 的前 i - 1位,记为 r;对于第 i 位,\(\gcd(x-r,2^i)\) == \(2^i\) 则 x 的第 \(i\) 位是 0
- 但询问是 x 加一个数,不能询问 \(x-r\);所以可以询问 \(\gcd(x-r+2^i,2^{i+1})==2^{i+1}\), 则 x 的第 i 位是 1
- 令 \(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的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #651 (Div. 2) B. GCD Compression(数论)
题目链接:https://codeforces.com/contest/1370/problem/B 题意 给出 $2n$ 个数,选出 $2n - 2$ 个数,使得它们的 $gcd > 1$ . ...
- Codeforces Round #767 (Div. 2)——B. GCD Arrays
B. GCD Arrays 题源:https://codeforces.com/contest/1629/problem/B 题目大意 给出一段区间[l, r],可以进行操作(把任意两个数拿出来,把他 ...
- 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 ...
- 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 ...
- Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)
对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i] ...
- Codeforces Round #651 (Div. 2) B. GCD Compression (构造)
题意:有一个长度为\(2n\)的数组,删去两个元素,用剩下的元素每两两相加构造一个新数组,使得新数组所有元素的\(gcd\ne 1\).输出相加时两个数在原数组的位置. 题解:我们按照新数组所有元素均 ...
- Codeforces Round #838 (Div. 2) D. GCD Queries
题意 有个长度为n的排列p,[0,1,2,...n-1],你可以进行至多2*n次询问,每次询问两个i,j,返回gcd(pi,pj),让你在规定时间内猜出0在哪两个位置之一 思路 这是一道交互题,询问的 ...
- 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 ...
随机推荐
- SAP 后台任务定时job
定时任务的事务码 sm36:创建定时任务 sm37:查看定时任务 JDBG:后台任务debug,在对应的sm37中对应的job页面 t-code输入 创建定时任务SM36 名称可以随便起 一般都是按自 ...
- Centos7.6centOS7.8多网卡多IP配置
1.进入网卡配置文件 cd /etc/sysconfig/network-scripts/2.创建新网卡的配置文件 ifcfg-eth1 cp ifcfg-eth0 ifcfg-eth1这样副网卡配置 ...
- appcrawler(2.1.3)采坑
转发:https://www.jianshu.com/p/d97290136bad 遇到的坑: 问题1: Exception in thread "main" org.openqa ...
- ES、Lucene、Solr的介绍和区别
一.介绍 1.ES Elasticsearch是用Java开发并且是当前最流行的开源的企业级搜索引擎,支持多种语言 2.Lucene Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索 ...
- Pytorch实战学习(七):高级CNN
<PyTorch深度学习实践>完结合集_哔哩哔哩_bilibili Advanced CNN 一.GoogLeNet Inception Module:而为了减少代码的冗余,将由(卷积(C ...
- Ubuntu之切换root用户提示认证失败
出现认证失败(Authentication failure)的原因有两种,要么是密码输入错误,要么是新安装的系统还没有给root设置密码,,如果是后者,可以通过命令sudo passwd root进行 ...
- Symfony2在Nginx下的配置方法图文教程
来源: https://www.xp.cn/b.php/79706.html Symfony2在Nginx下的配置方法图文教程 本文详细讲述了Symfony2在Nginx下的配置方法.分享给大家供大家 ...
- geoserver的自动化部署
年后接到一个任务,需求是这样的: 搭建一个geoserver服务器,将公司内部的mbtile数据(EPSG:3857)发布出去 服务的输出格式为MBTiles with vector tiles的矢量 ...
- 浏览器打开微信小程序
function h5() { $wx = new Wx('appId', 'appSecret'); // \dump($wx->getAccessToken()); $url = 'http ...
- 并发多线程学习(五)Java线程的状态及主要转化方法
1 操作系统中的线程状态转换 首先我们来看看操作系统中的线程状态转换. 在现在的操作系统中,线程是被视为轻量级进程的,所以操作系统线程的状态其实和操作系统进程的状态是一致的. 操作系统线程主要有以下三 ...