CF1665D GCD Guess

Luogu 链接
CodeForces 链接
Virtual Judge 链接

题意

这是一道 IO 交互题,请注意刷新缓冲区。


题目描述

现在有一个正整数 \(n\)\(1\le n\le10^9\)),你必须在 \(30\) 次询问内将其猜出。

每次询问,你可以选择不同的正整数 \(a\)\(b\),交互库将返回 \(\gcd(n+a,n+b)\)


交互格式

多测,第一行一个整数 \(T\)\(1\le T\le1000\)),代表有 \(T\) 组数据。

每次询问的格式为 ? a b,返回值见题目描述

当你确定答案时,请输出 ! n然后立即结束这组数据(进行下组数据或结束程序)。

思路

注意到我们需要在 \(30\) 次内猜出 \(n\) 的值,可以猜测我们需要得到 \(n\) 二进制下每一位的值。

为了方便计算,在判断第 \(i\) 位(下标从右往左,从 \(0\) 开始)的值时,先将第 \(0\)\(i-1\) 位清空(\(i=0\) 则不用清空)。

我们只需记录当前得到的 \(ans\),把要询问的 \(a\)\(b\) 都减去 \(ans\),即可将第 \(i\) 位以前的位清 \(0\)

\(n'=n-ans\),若 \(2^{i+1}\mid\gcd(n'+2^i,n'+2^i+2^{i+1})\),则可以判断 \(n'\) 的第 \(i\) 位为 \(1\)。以下是对这一结论的简略证明:

首先有 \(2^{i+1}\mid\gcd(n'+2^i,n'+2^i+2^{i+1})=\gcd(n'+2^i,2^{i+1})\),所以 \(2^{i+1}\mid n'+2^i\)

\(n'\)\(i\) 位为 \(1\),则显然有 \(2^{i+1}\mid n'+2^i\);若 \(n'\)\(i\) 位为 \(0\),则显然有 \(2^{i+1}\nmid n'+2^i\)

所以,当且仅当 \(n'\) 的第 \(i\) 位为 \(1\) 时,\(2^{i+1}\mid\gcd(n'+2^i,n'+2^i+2^{i+1})\) 成立。

综上,在判断第 \(i\) 位的值时,我们可以令 \(a=2^i-ans,b=2^i+2^{i+1}-ans\),然后 ? a b,若返回值能被 \(2^{i+1}\) 整除,则将 \(ans\) 加上 \(2^i\)

最后输出 ! ans

程序

AC 记录

#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<cmath>
#include<iomanip>
#include<string>
#include<stack>
#include<random>
#include<utility>
#define re register
#define ll long long
#define ull unsigned long long
#define vl __int128
#define ld long double
#define LL 2e18
#define INT 1e9
#define INF 0x3f3f3f3f
#define MP make_pair
#define pb push_back
#define lb(x) (x&(-x))
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
int T=1;
using namespace std;
pair<int*,int*> pair_int;pair<ll*,ll*> pair_ll;pair<ull*,ull*> pair_ull;pair<double*,double*> pair_double;pair<ld*,ld*> pair_ld;pair<char*,char*> pair_char;pair<bool*,bool*> pair_bool;
inline bool blank(const char x){return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
template<typename Tp>inline void read(Tp &x){x=0;bool z=true;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=(x<<1)+(x<<3)+(a^48);x=(z?x:~x+1);}
inline void read(double &x){x=0.0;bool z=true;double y=0.1;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}
inline void read(ld &x){x=0.0;bool z=true;ld y=0.1;char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}
inline void read(char &x){for(x=gc();blank(x)&&(x^-1);x=gc());}
inline void read(char *x){char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())*x++=a;*x=0;}
inline void read(string &x){x="";char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())x+=a;}
template<typename T>inline void read(pair<T*,T*> p){T *begin,*end,*i;begin=p.first,end=p.second;if(begin<end)for(i=begin;i<end;++i)read(*i);else for(i=begin-1;i>=end;--i)read(*i);}
template<typename T,typename ...Tp>inline void read(T &x,Tp &...y){read(x),read(y...);}
template<typename Tp>inline void write(Tp x){if(!x)return pc(48),void();if(x<0)pc('-'),x=~x+1;int len=0;char tmp[64];for(;x;x/=10)tmp[++len]=x%10+48;while(len)pc(tmp[len--]);}
inline void write(const double x){int a=6;double b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;double y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}
inline void write(const ld x){int a=6;ld b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;ld y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}
inline void write(const pair<int,double>x){int a=x.first;if(a<7){double b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;double y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else printf("%.*lf",a,x.second);}
inline void write(const pair<int,ld>x){int a=x.first;if(a<7){ld b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;ld y=5*powl(10,-a-1);b+=y,c+=y;int len=0;char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else printf("%.*Lf",a,x.second);}
inline void write(const char x){pc(x);}
inline void write(const bool x){pc(x?49:48);}
inline void write(char *x){fputs(x,stdout);}
inline void write(const char *x){fputs(x,stdout);}
inline void write(const string &x){fputs(x.c_str(),stdout);}
template<typename T>inline void write(pair<T*,T*> p){T *begin,*end,*i;begin=p.first,end=p.second;for(i=begin;i<end;++i)write(*i),write(' ');}
template<typename T>inline void write(pair<pair<T*,T*>,char> p){T *begin,*end,*i;begin=p.first.first,end=p.first.second;char c=p.second;for(i=begin;i<end;++i)write(*i),write(c);}
template<typename T,typename ...Tp> inline void write(T x,Tp ...y){write(x),write(y...);}
template<typename T>inline void init(T *begin,T *end,const T& val=T()){T* i;for(i=begin;i<end;++i)*i=val;}
template<typename T>inline T max(T *begin,T *end){T *ans,*i;for(i=begin;i<end;++i)if(i==begin||*ans<*i)ans=i;return *ans;}
template<typename T>inline T min(T *begin,T *end){T *ans,*i;for(i=begin;i<end;++i)if(i==begin||*i<*ans)ans=i;return *ans;}
template<typename T>inline T calc_sum(T *begin,T *end,const T& val=T()){T ans=val,*i;for(i=begin;i<end;++i)ans+=*i;return ans;}
template<typename T>inline bool is_equal(T *begin,T *end,const T& val=T()){T *i;for(i=begin;i<end;++i)if(*i!=val)return false;return true;}
template<typename T>inline T qp(T x,T y){T ans=1;while(y){if(y&1)ans=ans*x;x=x*x;y>>=1;}return ans;}
template<typename T>inline T qp(T x,T y,T z){T ans=1;while(y){if(y&1)ans=ans*x%z;x=x*x%z;y>>=1;}return ans;}
template<typename T>inline T abs(T x){return x>=0?x:-x;}

ll mod=0;
const int MAXN=1e5;
const int N=MAXN+10;
//#define DEBUG
#define more_text

int g,ans;

void ask(int a,int b,int &x){
	printf("? %d %d\n",a,b);fflush(stdout);
	scanf("%d",&x);
}
int answer(int a){
	printf("! %d\n",a);fflush(stdout);
}
void SOLVE(int _){
	ans=0;
	for(int i=0;i<30;++i){
		ask((1<<i)-ans,(3<<i)-ans,g);
		if(g%(2<<i)==0)ans+=1<<i;
	}
	answer(ans);
}
/*
Input:

Output:

Outline:

*/
int main(){
	#ifdef DEBUG
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
	#endif
	#ifdef more_text
	read(T);
	#endif 
	for(int i=1;i<=T;++i)SOLVE(i);
	#ifdef DEBUG
	fclose(stdin);fclose(stdout);
	#endif
	return 0;
}
posted @ 2025-03-11 20:32  LXcjh4998  阅读(12)  评论(0)    收藏  举报