Codeforces Round #838 (Div. 2) D. GCD Queries
2024-10-20 20:30:46
题意
有个长度为n的排列p,[0,1,2,...n-1],你可以进行至多2*n次询问,每次询问两个i,j,返回gcd(pi,pj),让你在规定时间内猜出0在哪两个位置之一
思路
这是一道交互题,询问的上限是2n次
通过三个数,可以去除掉一个不是0的数
对三个数进行以下询问,gcd(a,i),gcd(b,i)
如果gcd(a,i) != gcd(b,i),那么其中a,b小的被i取代,因为a,b中假如有0,那么一定是大的数,那么小的数一定不是0
如果gcd(a,i) == gcd(b,i),那么跳过i,因为假如i是0,因为数列每个数都不同,所必不可能相等
那么for一遍数组,每次和当前位置i进行两次询问,最多2n的限制内就可以筛选出0可能存在的位置p,q
代码
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e4 + 10;
int n, t = 1;
long long a[N], b[N];
int pw[N];
int l[N], cnt[N];
const int mod = 998244353;
map<int, int> mp;
int v[N];
int ask(int x, int y) {
cout << "? " << x << ' ' << y << endl;
int u;
cin >> u;
return u;
}
void ok(int x, int y) {
cout << "! " << x << ' ' << y << endl;
int u;
cin >> u;
}
void run() {
cin >> n;
int p = 1, q = 2;
for (int i = 3; i <= n; i++) {
int r1 = ask(p, i), r2 = ask(q, i);
if (r1 < r2) {
p = i;
}
if (r1 > r2) {
q = i;
}
}
ok(q, p);
}
int main() {
srand(time(0));
cin >> t;
while (t--)
run();
return 0;
}
最新文章
- 《Paxos Made Simple》翻译
- .this语句指的是什么
- c语言->;和 .
- Mac 安装 home Brew以及 XCTool的过程记录
- 三、第一个Struts2应用案例(编码步骤)
- HTML、JS、CSS之特殊字符
- IntelliJ IDEA “Finds duplicated code”提示如何关闭
- IDEA类文件不编译问题
- WKWebView 官方文档
- [GXOI/GZOI2019]与或和
- Android中 Git 使用中几个概念
- System.Collections里的一些接口
- InheritParasitic.js
- Python - 列表解析式
- codeforces365B
- Java8 异步编排类CompletableFuture
- K8S笔记
- 【LOJ】#2117. 「HNOI2015」实验比较
- Redis的简介
- windows10 装linux子系统