Tickets

https://loj.ac/problem/10041

Title Description

  Given a number of columns, A 0 =. 1, A I +. 1  = (A × A I + A I MOD B) MOD C. Seeking a first reference duplicates occur.

Thinking

  Because the answers given will not exceed 10 × 2 6 each, so we can enumerate the number of violent row, and then quickly determine whether there can be. Obviously this question can map out the past, but a hash table is also a good choice. We take the results of an appropriate analog to digital, and then insert the hash table, then you can find, because the number of columns will not be too long, so you can quickly find. Of course, you can optimize use open addressing method, but an array of open larger chain not too long and not T.

Code

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull mod=2e6+3;
ull nxt[2000005],poi[mod+5],key[2000005],tot;
void insert(ull x)
{
    ull h=x%mod;
    nxt[++tot]=poi[h];
    poi[h]=tot;
    key[tot]=x;
}
bool check(ull x)
{
    ull h=x%mod;
    for(int i=poi[h];i;i=nxt[i])
        if(key[i]==x)return 1;
    return 0;
}
int main() 
{
    ull a,b,c,cnt,x;
    scanf("%llu%llu%llu",&a,&b,&c);
    x=1;cnt=0;
    while(1)
    {
        insert(x);
        x=(a*x+x%b)%c;
        cnt++;
        if(check(x)||cnt>2000000)break ;
    }
    if(cnt>2000000)printf("-1");
    else printf("%llu",cnt);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fangbozhen/p/11621868.html