fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. ll a,b,c;
  6. ll f(ll x){
  7. return(a * x + x / b) % c;
  8. }
  9. int main(){
  10. cin >> a >> b >> c;
  11. ll r,t;
  12. t = f(1);
  13. r = f(f(1));
  14.  
  15. while(r != t){
  16. t = f(t);
  17. r = f(f(r));
  18. }
  19.  
  20. ll ans1 = 0;
  21. t = 1;
  22.  
  23. while(r != t){
  24. r = f(r);
  25. t = f(t);
  26. ans1++;
  27. }
  28.  
  29. ll ans2 = 1;
  30. t = f(t);
  31. while(r != t){
  32. t = f(t);
  33. ans2++;
  34. }
  35. cout << ans1 + ans2;
  36. }
  37.  
Success #stdin #stdout 0s 5320KB
stdin
8 2 31
stdout
9