月赛T2,hack我自己

学术版

InterN_NOT_FOUND @ 2024-10-02 18:30:50

rt,code:

#include<bits/stdc++.h>

namespace INstd {
    std::string Set_Nsolution = "-1";
    #define int __int128
    #define NO_SOLUTION {cout << Set_Nsolution;return 0;}

    namespace mathematicsh{
        inline int gcd(int a,int b){
            return (b==0?a:gcd(b,a%b));
        }
    }

    namespace IO{
        inline bool isnum(char ch){return ch>='0'&&ch<='9';}
        inline int read()
        {
            int x=0,f=1;char ch=getchar();
            while (!isnum(ch)){if (ch=='-') f=-1;ch=getchar();}
            while (isnum(ch)){x=x*10+ch-48;ch=getchar();}
            return x*f;
        }
        inline void out(int x,char ch){
            if(x<0){putchar('-');x=-x;}
            if(x>9)out(x/10,'/');
            putchar(x%10+'0');
            if(ch=='l')putchar('\n');
            if(ch=='s')putchar(' ');
        }
    }

    namespace Debug{
        #define err() cout<<"err "<<__LINE__<<endl,exit(0)
        #define pot(args...) \
        GPT(#args),cout<<"  Line "<<__LINE__<<"\t: ", \
        PPT(args),cout<<"\n\n"
        void PPT(){}
        template<typename TYPE,typename... TYPES>
        void PPT(const TYPE& x,const TYPES&... y){
            std::cout<<x<<' ';
            PPT(y...);
        }
        void GPT(std::string nam){std::cout<<std::setw(29)<<nam;}
    }

    using namespace std;
    using namespace mathematicsh;
    using namespace IO;
    using namespace Debug;
}

using namespace INstd;

const int mx = 4e8, prm1 = 399999959, prm2 = 399999949;

int T = read();

inline int Exgcd (int a, int b, int &x, int &y) {
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    int d = Exgcd(b, a % b, x, y);
    int t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
}

signed main()
{
    fflush(stdout);
    while (T --) {
        int x, y;
        printf("? "); out(prm1, 'l');
        fflush(stdout);
        x = read();
        fflush(stdout);

        printf("? "); out(prm2, 'l');
        fflush(stdout);
        y = read();
        fflush(stdout);

        if (x == y) {
            printf("! "); out(x, 'l');
            fflush(stdout);
            continue;
        }

        int xx = 0, yy = 0;
        Exgcd(prm1, prm2, xx, yy);
        xx *= (y - x); yy *= -(y - x);
        int k1, k2;

        k1 = (xx > 0 && xx % prm2 != 0) ? xx % prm2 : xx % prm2 + prm2;
        k2 = (yy > 0 && yy % prm1 != 0) ? yy % prm1 : yy % prm1 + prm1;

        int ans1 = k1 * prm1 + x, ans2 = k2 * prm2 + y;

        printf("! "); 

        if (ans1 % prm1 == x && ans2 % prm2 == y)
            out(ans1, 'l');
        else out(ans2, 'l');

        fflush(stdout);
    }
    return 0;
}

思路就是弄两坨大质数跑扩欧,但是我在测试手造样例时发现一个问题

我假设 m=399999958 ,运行过程:

1
? 399999959
399999958
? 399999949
9
! 159999963600002049

而且还AC了,求问


by InterN_NOT_FOUND @ 2024-10-02 18:32:38

@ran_qwq 开了

#define int __int128


by Autumn_Rain @ 2024-10-02 18:34:51

@InterN_NOT_FOUND 不用大质数,拿4e8和4e8-1就行了


by Autumn_Rain @ 2024-10-02 18:35:06

@InterN_NOT_FOUND

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=4e8;
ll T,mod,a[10],b[10],n;
ll exgcd(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x=1;y=0;
        return a;
    }
    ll d=exgcd(b,a%b,x,y);
    ll temp=x;
    x=y;
    y=temp-a/b*y;
    return d;
}
ll CRT(ll *W,ll *B,ll k){
    ll x,y,a=0,m,n=1;
    for(ll i=1;i<=k;i++)n*=W[i];
    for(ll i=1;i<=k;i++){
        m=n/W[i];
        exgcd(W[i],m,x,y);
        a=(a+y*m*B[i])%n;
    }
    return (a>0?a:a+n)%n;
}
void solve(){
    n=mod=0;
    for(int i=1;i<=5;i++){
        a[i]=b[i]=0;
    }
    ll fi=N,se=N-1;
    ll t1,t2;
    cout<<"? "<<fi<<endl;
    cin>>mod;t1=mod;
    a[++n]=fi;b[n]=mod; 
    cout<<"? "<<se<<endl;
    cin>>mod;t2=mod;
    a[++n]=se;b[n]=mod;
    if(t1==t2&&t1==0){
        cout<<"! 0\n";
        return;
    }
    cout<<"! "<<CRT(a,b,2)<<endl;
}
int main(){
    cin>>T;
    while(T--)solve();
    return 0;
}

by Infinity_Fantasy @ 2024-10-02 18:36:56

T2?是B1吗?为啥要咋么长?


by InterN_NOT_FOUND @ 2024-10-02 18:37:45

@Autumn_Rain 这样我又跑出个新的问题

假设m=399999999

1
? 400000000
399999999
? 399999999
0
! 159999999999999999

by InterN_NOT_FOUND @ 2024-10-02 18:38:19

@Infinity_Fantasy 码风问题


by CH_mengxiang @ 2024-10-02 18:38:35

@Infinity_Fantasy 一大半都是自己的惯用模板扔命名空间里了


by CH_mengxiang @ 2024-10-02 18:39:20

这题我直接拿数值范围最后两个数询问的,莫名其妙就过了,不过B2这种方法用不了了好像


by Infinity_Fantasy @ 2024-10-02 18:39:42

@InterN_NOT_FOUND 跟码风没关系吧,至少我不需要exgcd


by Infinity_Fantasy @ 2024-10-02 18:40:37

@CH_mengxiang 快读和调试我看不出来吗?我就是指做法啊


| 下一页