A.cpp 730 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using LL=long long;
  4. LL e_gcd(LL a, LL b, LL &x, LL &y) {
  5. if (b == 0) {
  6. x = 1;
  7. y = 0;
  8. return a;
  9. }
  10. LL ans = e_gcd(b, a % b, x, y);
  11. LL temp = x;
  12. x = y;
  13. y = temp - a / b * y;
  14. return ans;
  15. }
  16. LL cal(LL a, LL b, LL c) {
  17. LL x, y;
  18. LL gcd = e_gcd(a, b, x, y);
  19. if (c % gcd != 0) return -1;
  20. x *= c / gcd;
  21. b /= gcd;
  22. b = abs(b);
  23. LL ans = x % b;
  24. if (ans <= 0) ans += b;
  25. return ans;
  26. }
  27. int main() {
  28. for (LL x, y, m, n, L; cin >> x >> y >> m >> n >> L;) {
  29. LL ans = cal(m - n, L, y - x);
  30. if (ans == -1) cout << "Impossible" << endl;
  31. else cout << ans << endl;
  32. }
  33. return 0;
  34. }