「题解」「CF1103B」Game with modulo
简易中文题目
猜一个数字 ,而你可以向机器提问一对 ,如果 机器返回字符串 x
,反之返回字符串 y
。
询问不能超过 次,请你猜出 。
解析
一道十分巧妙的数学题+交互题(人生第二道交互题)。
考虑我们询问的数对是 ,那么就有:
- ,那么
- ,那么 ,又因为 那么
- ,无法分析。
发现第三种情况无法分析,怎么办?
尽可能从小开始枚举 ,这样就尽可能避免 的情况发生。
考虑从 开始枚举,如果过程返回 y
,那么我们枚举小了,继续扩大 ,反之说明 ,在这个区间之内做二分即可。
但是我们怎么枚举 呢?其实有多种方法,这里我推荐使用倍增 因为它的时间复杂度好算 。
即 按照 枚举,这样可以在 的时间内求出 的大致范围。
但是由于我们的 是从 开始枚举的,而 属于第三种情况,我们无法处理,所以需要特判 。
另外,如果是使用 printf()
的大佬需要在每一次输出之后用 fflush(stdout)
清空一下输出缓冲区的东西,不然会出现玄学错误。
剩下的就是代码实现了。
代码
切莫直接 copy
,他好,你也好[手动滑稽]。
#include<cstdio>
#include<cstring>
#define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
#define int long long
// #define int unsigned
#ifdef FILEOI
# define MAXBUFFERSIZE 500000
inline char fgetc(){
static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
inline int qread(){
int x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);
putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}
const int MAXA=1e9;
char s[105],res[105];
int l,r,mid;
inline bool Compare(const char a[],const char b[]){
int la=strlen(a),lb=strlen(b);
if(la^lb)return false;
rep(i,0,la-1)if(a[i]!=b[i])return false;
return true;
}
inline bool Ask(const int x,const int y){
printf("? %lld %lld\n",x,y);fflush(stdout);
scanf("%s",res);
return res[0]=='x';
}
signed main(){
#ifdef FILEOI
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
#endif
while(233333){
scanf("%s",s);if(Compare(s,"end"))break;
//特判 1
if(Ask(0,1)){
printf("! 1\n");fflush(stdout);
continue;
}
for(int i=1;i<=MAXA;i<<=1)if(Ask(i,Min(i<<1,MAXA<<1))){
l=i,r=Min(i<<1,MAXA<<1);
break;
}
while(l+1<r){
mid=(l+r)>>1;
if(Ask(l,mid))r=mid;
else l=mid;
}
printf("! %lld\n",r);fflush(stdout);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现