fork(1) download
  1. // asdasdasda as dasd
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define REP(a,b,c) for(int a=b;a<c;a++)
  5. #define asd(x) cout<<__LINE__<<" :: "<<#x<< ": "<<x<<endl;
  6. #define asdf(x, y) cout<<__LINE__<<" :: "<<#x<< ": "<<x<<" | "<<#y<< ": "<<y<<endl;
  7. #define lb() cout << string(15, =) << endl
  8. typedef pair<int,int> ii;
  9. typedef long long LL;
  10.  
  11. long long A, B, mod;
  12.  
  13. long long next(long long a){
  14. return (a*A + a%B)%mod;
  15. }
  16.  
  17. void yolo(long long slow){
  18. int cyclelen = 1, len = 0;
  19. long long a = slow, b = slow;
  20. while(next(a) != b){
  21. a = next(a);
  22. cyclelen++;
  23. }
  24.  
  25. a = 1, b = 1, len = cyclelen;
  26. while(len--) b = next(b);
  27. while(a != b) a = next(a), b = next(b), cyclelen++;
  28.  
  29. cout << cyclelen << endl;
  30. exit(0);
  31. }
  32.  
  33. int main(){
  34.  
  35. freopen("input.txt", "r", stdin);
  36. freopen("output.txt", "w", stdout);
  37.  
  38. cin >> A >> B >> mod;
  39. long long slow = 1, fast = 1, iter = 2e7;
  40.  
  41. while(iter){
  42. slow = next(slow);
  43. fast = next(next(fast));
  44. if(slow == fast) yolo(slow);
  45. iter--;
  46. }
  47.  
  48. cout << -1 << endl;
  49. return 0;
  50. }
Runtime error #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Standard output is empty