You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
859 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
// 扩展欧几里得算法
int exgcd(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
signed main() {
int a, b, m, n, l;
// 青蛙 A 的出发点坐标是 a青蛙 B 的出发点坐标是 b
// 青蛙 A 一次能跳 m 米,青蛙 B 一次能跳 n 米
// 纬度线总长 L 米
cin >> a >> b >> m >> n >> l;
int x, y;
int d = exgcd(m - n, -l, x, y);
if ((b - a) % d)
puts("Impossible"); // 永远也碰不到
else {
x = (b - a) / d * x; // 按照比例扩大
int t = abs(l / d);
cout << (x % t + t) % t << endl; // 返回正的余数
}
}