2 solutions

  • 0
    @ 2024-9-14 10:14:51

    #include <bits/stdc++.h> using namespace std; #define LL long long const int N = 1e6 + 10; const int INF = 0x3f3f3f3f; const int maxn = 2200000, Mod = 2181271; int Hash[maxn]; struct Point { LL val; int next; }p[maxn]; bool count(int pos, LL x) { for (int i = Hash[pos]; i; i = p[i].next) if (p[i].val == x) return true; return false; } int A, B, C; int main() { LL a = 1; Hash[1] = p[1].val = 1; scanf("%d%d%d", &A, &B, &C); for (int i = 1; i <= 2000000; i++) { a = (a * A + a % B) % C; int pos = a % Mod; if (count(pos, a)) { printf("%d\n", i); return 0; } p[i + 1] = { a,Hash[pos] }; Hash[pos] = i + 1; } printf("-1\n"); return 0; }

  • -1
    @ 2024-7-23 13:09:48
    #include<iostream>
    using namespace std;
    int flag[1000000001];
    int main()
    {
        long long a,b,c,back=1,as;
        flag[1]=true;
        cin>>a>>b>>c;
        for(int i=1;i<=2000000;i++)
        {
            as=(a*back+back%b)%c;
            //cout<<as;
            if(flag[as]==1)
            {
                cout<<i;
                return 0;
            }
            else
            {
                back=as;
                flag[as]=1;
            }
        }
        cout<<-1;
        return 0;
    }
    
    • 1

    Information

    ID
    1117
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    8
    Tags
    (None)
    # Submissions
    72
    Accepted
    12
    Uploaded By