1 条题解
-
0
思路
a0为1即使C=1也不能预先取模;首重复编号为环前缀加周期,超过200万才输出-1,常数空间检测。
参考程序(C++11)
#include <bits/stdc++.h> using namespace std; int main(){long long a,b,c;cin>>a>>b>>c;auto f=[&](long long x){return (a*x+x%b)%c;};int cap=2000000,steps=1;long long x=f(1),y=f(f(1));while(x!=y&&steps<=cap){x=f(x);y=f(f(y));steps++;}if(steps>cap){cout<<-1<<'\n';return 0;}int mu=0;x=1;while(x!=y){x=f(x);y=f(y);if(++mu>cap){cout<<-1<<'\n';return 0;}}int len=1;y=f(x);while(x!=y){y=f(y);if(++len+mu>cap){cout<<-1<<'\n';return 0;}}cout<<(mu+len>cap?-1:mu+len)<<'\n';}
- 1