/* Telekinetic Forest Guard */
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
inline void exgcd(LL &x, LL &y, LL a, LL b) {
b ? (exgcd(y, x, b, a % b), y -= a / b * x) : (x = 1, y = 0);
}
int main() {
LL a, b, n, m, L;
scanf("%lld%lld%lld%lld%lld", &a, &b, &n, &m, &L);
LL q = n - m, p = b - a;
while(q < 0) q += L;
while(p < 0) p += L;
LL d = __gcd(q, L);
if(p % d) {
printf("Impossible\n");
return 0;
}
LL x, y;
exgcd(x, y, q / d, L / d);
x = x * p / d % L;
if(x < 0) x += L;
printf("%lld\n", x);
return 0;
}
【BZOJ1477】青蛙的约会【扩展欧几里得】
最新推荐文章于 2018-12-01 20:53:00 发布