【题目链接】
/* 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; }