Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define LIM 20000000
- using namespace std;
- typedef long long ll;
- ll A,B,C;
- #define mod(x,y) ((x) >= (y) || (x) <= -(y) ? (x)%(y) : (x))
- #define F(x) mod((A*(x) + mod(x,B)),C)
- ll floyd(){
- ll tortoise = F(1ll), hare = F(F(1ll));
- while(tortoise != hare){ tortoise = F(tortoise); hare = F(F(hare)); }
- int mu = 0; hare = 1ll;
- while(tortoise != hare && mu <= LIM){
- tortoise = F(tortoise);
- hare = F(hare);
- mu++;
- }
- if(mu > LIM) return -1;
- int lambda = 1; hare = F(tortoise);
- while(tortoise != hare && lambda <= LIM){
- hare = F(hare);
- lambda++;
- }
- if(lambda > LIM || lambda + mu > LIM) return -1;
- return lambda + mu;
- }
- int main(){
- ios_base::sync_with_stdio(0);
- #ifdef ONLINE_JUDGE
- freopen("input.txt", "rb", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- while(cin >> A >> B >> C){
- cout << floyd() << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement