过了一年多了,终于把这题过了。
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll exgcd(ll a, ll b, ll &x, ll &y)//扩展欧几里得
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
ll r = exgcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - a / b * y;
return r;
}
ll solve(ll a, ll b, ll c, ll &x, ll &y) {
ll d = exgcd(a, b, x, y);
if(c % d != 0)
return -1;
x = x * (c / d);
ll k = (-x) / (b / d);
x += k * (b / d);
if(x <= 0)
x += b;
return x;
}
int main()
{
ll x, y, m, n, L, a, b, d;
scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L);
ll ans = solve(m - n, L, y - x, a, b);
if(ans == -1)
printf("Impossible\n");
else
printf("%lld\n", ans);
return 0;
}