首页 > 其他分享 >Codeforces Round #838 (Div. 2)-D. GCD Queries-GCD、交互

Codeforces Round #838 (Div. 2)-D. GCD Queries-GCD、交互

时间:2023-02-04 11:48:45浏览次数:64  
标签:gcd int 838 Codeforces GCD query r1

题目:https://codeforces.com/problemset/problem/1762/D

有一个0~n-1的排列,你要在至多2n次询问中找到两个位置x,y,使得\(p_x,p_y\)至少有一者为0.每次询问可以问两个不同的i,j,测评机会回答\(\gcd(p_i,p_j)\)的值。


题解:
\(\gcd(0,x)=x\)
以及这题一个很关键的地方是,我不需要确定具体哪个是0,只需要保证两个中有一个是0就可以了

这样子我们考虑如何“排除某个地方一定不是0”,假设有三个位置\(i,j,k\):

  • 若\(\gcd(p_i,p_k)=\gcd(p_j,p_k)\),则\(p_k\)一定非零,否则\(p_i=p_j\)和全排列矛盾。
  • 若\(\gcd(p_i,p_k)>\gcd(p_j,p_k)\),则\(p_j\)一定非零,否则\(\gcd(p_j,p_k)=p_k\geq \gcd(p_i,p_k)\)。
  • 若\(\gcd(p_i,p_k)<\gcd(p_j,p_k)\),同理
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
using namespace std;
int query(int x,int y){ 
	cout<<"? "<<x<<' '<<y<<endl;
	fflush(stdout);
	int r;cin>>r;return r;
}
int main(){
	int T;cin>>T;
	while(T--){
		int n;cin>>n;
		int x=1,y=2;
		rep(i,3,n){
			int r1=query(i,x);
			int r2=query(i,y);
			if(r1>r2)y=i;
			else if(r1<r2)x=i;
		}
		cout<<"! "<<x<<' '<<y<<endl;
		fflush(stdout);
		int ret;cin>>ret;
	}
	return 0;
}

标签:gcd,int,838,Codeforces,GCD,query,r1
From: https://www.cnblogs.com/yoshinow2001/p/17091174.html

相关文章

  • CodeForces - 234E Champions' League(模拟)
    Description: Intheautumnofthisyear,twoRussianteamscameintothegroupstageofthemostprestigiousfootballclubcompetitionintheworld—theUEFA......
  • CodeForces - 224D Two Strings
    Description:A subsequence oflength |x| ofstring s = s1s2... s|s| (where |s| isthelengthofstring s)isastring x = sk1sk2... sk|x| (1 ≤......
  • codeforces 1047C. Enlarge GCD(数学)
    题意:给出n个数,求出最少删除几个数可以扩大最大公因子。AC代码:#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<set>#include<map>#includ......
  • CodeForces 948A
    DescriptionBobisafarmer.Hehasalargepasturewithmanysheep.Recently,hehaslostsomeofthemduetowolfattacks.Hethusdecidedtoplacesomeshephe......
  • Codeforces 1322 B. Count Subrectangles(贪心)
    题意:给出序列和序列矩阵是由和决定的问你可以从中截取多少个面积为显然可知的一个性质那就是当序列连续长度为,序列连续长度为,时这个部分序列形成的......
  • Codeforces 1322 A. Unusual Competitions
    题意:给出一个含有的字符串,让你可以选择一个区间进行重新排序,问一共选择的区间长度是多少可以使得字符串最后变成我们只需要从头开始遍历然后找到这种字符,并且使得和......
  • Codeforces 1316 B. String Modification
    题意:反转一个字符串,反转规则为从字符串首字母开头,长度为,反转问一个$k$时会得到一个新串,求字典序最小的新串和,如果字典序相同,则输出最小的。比赛时被卡,疯狂,其实举......
  • Codeforces 1316 D. Nash Matrix(dfs)
    题意:给出一个的棋盘和每个棋盘位置最后能走到的位置,如果一直走不停下来就是,可以停下来就是走到的最后位置,让你输出每个位置的操作字符,上下左右和,停在此位置。我们先找......
  • 扩展欧几里得(exgcd)
    这里先说一下最大公约数怎么求,辗转相除法都会用,这里讲一下站桩相除法的原理。例如两个数假设这两个数大小,设是这两个数的最大公约数。可得因为这里都是一个正整数不会对最......
  • Codeforces1260 E Tournament(贪心)
    Description:Youareorganizingaboxingtournament,wherenboxerswillparticipate(ispowerof),andyourfriendisoneofthem.Allboxershavedifferents......