fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5.  
  6. ll eg(ll a, ll b, ll &x, ll &y){
  7. return b ? (eg(b, a % b, y, x), y -= a / b * x) : (x = 1, y = 0, a);
  8. }
  9.  
  10. int main() {
  11. ios::sync_with_stdio(false);
  12. cin.tie(nullptr);
  13.  
  14. freopen("CHASE.INP","r",stdin);
  15. freopen("CHASE.OUT","w",stdout);
  16.  
  17. ll M, xh, xt, h, t;
  18. cin >> M >> xh >> xt >> h >> t;
  19.  
  20. ll A = (h - 2*t) % M;
  21. ll B = (xt - xh) % M;
  22.  
  23. A = (A + M) % M;
  24. B = (B + M) % M;
  25.  
  26. ll x, y, d = eg(A, M, x, y);
  27.  
  28. if (B % d) return cout << -1, 0;
  29.  
  30. ll mod = M / d;
  31. cout << ((B / d) * (x % mod + mod) ) % mod;
  32. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Standard output is empty