1 条题解

  • 0
    @ 2025-10-8 16:53:44

    map(超时):

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=2e6;
    int main()
    {
        LL A,B,C;scanf("%lld%lld%lld",&A,&B,&C);
        unordered_map<LL,bool>mp;
        mp[1]=true;
        for(LL i=1,x=1;i<=N;i++)
        {
            x=(A*x%C+x%B)%C;
            if(mp[x]==true){printf("%d\n",i);return 0;}
            else mp[x]=true;
        }
        puts("-1");
        return 0;
    }
    

    自建Hash(AC):

    #include<bits/stdc++.h>//自建hash快又省空间
    using namespace std;
    typedef long long LL;
    const int N=2e6;
    struct node{LL x;int pre;}a[N+10];int len,last[N+10];
    void ins(LL x){++len;a[len]={x,last[x%N]};last[x%N]=len;}
    bool check(int x)
    {
        for(int k=last[x%N];k;k=a[k].pre)if(a[k].x==x)return true;
        return false;
    }
    int main()
    {
        LL A,B,C;scanf("%lld%lld%lld",&A,&B,&C);
        len=0;memset(last,0,sizeof(last));
        ins(1);
        for(LL i=1,x=1;i<=N;i++)
        {
            x=(A*x+x%B)%C;
            if(check(x)){printf("%d\n",i);return 0;}
            ins(x);
        }
        puts("-1");
        return 0;
    }
    
    • 1

    *【STL:unordered_map】查找数字(门票)

    信息

    ID
    815
    时间
    1000ms
    内存
    128MiB
    难度
    8
    标签
    递交数
    341
    已通过
    43
    上传者