题解:P16457 [UOI 2026] Guess the Number

· · 题解

这道交互题挺有趣的。看到这道题后,发现询问 1 时,就可以知道首位是多少了。但是怎么算后几位是难的,考虑乘法发现没办法做,只能考虑除法,用猜测词乘上当前得到的数分之一,观察到首位与猜测词相同时,若小于等于猜测词则首位是 1,若大于则不等于 1。

此时就可以进行二分了。但是要注意因为没有小数,所以当前数分之一要乘一个大数,大概是 10^{16}。

最后只用把尾数的 0 全部删掉就是答案了。

#include<bits/stdc++.h>
#define int long long
#define up(i,x,y) for(register int i=x;i<=y;i=-~i)
#define dn(i,y,x) for(register int i=y;i>=x;i--)
#define mst(x,y) memset(x,y,sizeof x)
#define D(x) cout<<#x<<": "<<x<<endl;
#define DE(x) cout<<#x<<": "<<x<<" ";
#define MAXSIZE 1<<21
// #define faster 1
#define pl p<<1
#define pr p<<1|1
using namespace std;
namespace lry{
    char buf[MAXSIZE],*p1=buf,*p2=buf;
    char pbuf[MAXSIZE],*pp=pbuf;
    inline int gc(){
        #if faster
            return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
        #else
            return getchar();
        #endif
    }
    inline void pc(const char &c){
        #if faster
            if(pp-pbuf==MAXSIZE) fwrite(pbuf,1,MAXSIZE,stdout),pp=pbuf;*pp++=c;
        #else
            putchar(c);
        #endif
    }
    inline bool blank(const char x){return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
    inline void read(double &rdx){double t=0;int x=0,s=0,f=1;char c;do c=gc();while(!isdigit(c)&&c!='-'&&c!='.');if (c=='-')f=-1,c=gc();while(isdigit(c)&&c!='.')x=(c^48)+(x<<1)+(x<<3),c=gc();if(c=='.')c=gc();else{rdx=x*f;return;}while(c>='0'&&c<='9')t=t*10+(c^48),s=-~s,c=gc();while(s--)t/=10;rdx=(x+t)*f;}
    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;}
    inline void read(int &x){int ret=0,f=0;char ch=gc();while(!isdigit(ch)){if(ch=='-')f=1;ch=gc();}while(isdigit(ch)){ret=(ret<<1)+(ret<<3)+(ch^48);ch=gc();}x=(f?-ret:ret);}
    template<typename t,typename ...T> inline void read(t &x,T&...y){read(x),read(y...);}
    inline void write(int x){if(x<0)pc('-'),x=-x;if(x>9)write(x/10);pc(x%10+48);}
    inline void write(const char *x){fputs(x,stdout);}
    inline void write(const string &x){fputs(x.c_str(),stdout);}
    inline void writeln(int x){write(x),pc('\n');}
    inline void writetr(int x){write(x),pc(' ');}
    inline void writeln(string x){write(x),pc('\n');}
    inline void writetr(string x){write(x),pc(' ');}
    template<typename t,typename ...T> inline void write(t x,T...y){write(x),write(y...);}
}
using namespace lry;
constexpr int N=500000+10,inf=0x3f3f3f3f3f3f3f3f,mod=10086001,mx=10000000000000000ll;
int T,q,n=11,a;
inline int fir(int x){
    int res=0;
    x*=n;
    while(x) res=x%10,x/=10;
    return res;
}
inline int guess(int x){
    pc('?'); pc(' '); writeln(x);
    fflush(stdout);
    int tmp; read(tmp);
    return tmp;
}
signed main(){
    read(T,q);
    while(T--){
        int mul=1,cnt=1;
        a=guess(1);
        int l=a*10000000,r=a*10000000+9999999,res=a*10000000;
        while(l<=r){
            int mid=(l+r)>>1;
            if(guess(mx/mid+(mx%mid!=0))==1) l=mid+1,res=mid;
            else r=mid-1;
            cnt++;
        }
        while(res%10==0) res/=10;
        pc('!'); pc(' '); writeln(res);
        fflush(stdout);
        // cout<<cnt<<endl;
    }
    #if faster
        fwrite(pbuf,1,pp-pbuf,stdout);
        pp=pbuf;
    #endif
    return 0;
}