//POJ 1061
//扩展欧几里得算法
/*
求解ax + by = c, 可先解得ax + by = gcd(a, b)
与等式右边无关,可得结论x = x0 + kb/gcd(a, b).
为了求解最小非负解,可以先取模在加上模值再取模得到
*/
void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (!b) d = a, x = 1, y = 0;
else exgcd(b, a%b, d, y, x), y -= x*(a / b);
}
int main() {
LL x, y, m, n, L, X, Y, d;
while (cin >> x >> y >> m >> n >> L) {
if (n < m) swap(n, m), swap(x, y);
exgcd(n - m, L, d, X, Y);
if ((x - y) % d) printf("Impossible\n");
else {
LL r = L / d;
LL ans = (X*(x - y) / d%r + r) % r;
//printf("X = %lld r = %lld\n", X, r);
printf("%lld\n", ans);
}
}
return 0;
}
POJ 1061 青蛙的约会
最新推荐文章于 2019-11-14 14:07:26 发布