/*
扩展欧几里德求模线性方程
感谢logic_space的指正
*/
#include <iostream>
#define abs(a) ((a)<0?-(a):(a))
using namespace std;
__int64 exGCD(__int64 a, __int64 b, __int64 &x, __int64 &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
__int64 r = exGCD(b, a%b, x, y);
__int64 t = x;
x = y;
y = t - a / b * y;
return r;
}
bool linear_equation(__int64 a, __int64 b, __int64 c, __int64 &x, __int64 &y)
{
__int64 n = exGCD(a, b, x, y);
if (c % n) return false;
__int64 k = c / n, t = abs(b/n);
x *= k;
y *= k;
x = (x % t + t) % t;
return true;
}
int main()
{
__int64 x, y, m, n, l;
__int64 ans, tmp;
scanf("%I64d %I64d %I64d %I64d %I64d", &x, &y, &m, &n, &l);
if (m == n)
{
printf("Impossible\n");
return 0;
}
if (!linear_equation(n-m, l, x-y, ans, tmp))//(n-m) * ans + l * tmp = x-y
printf("Impossible\n");
else
printf("%I64d\n", ans);
return 0;
}
扩展欧几里德求模线性方程
感谢logic_space的指正
*/
#include <iostream>
#define abs(a) ((a)<0?-(a):(a))
using namespace std;
__int64 exGCD(__int64 a, __int64 b, __int64 &x, __int64 &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
__int64 r = exGCD(b, a%b, x, y);
__int64 t = x;
x = y;
y = t - a / b * y;
return r;
}
bool linear_equation(__int64 a, __int64 b, __int64 c, __int64 &x, __int64 &y)
{
__int64 n = exGCD(a, b, x, y);
if (c % n) return false;
__int64 k = c / n, t = abs(b/n);
x *= k;
y *= k;
x = (x % t + t) % t;
return true;
}
int main()
{
__int64 x, y, m, n, l;
__int64 ans, tmp;
scanf("%I64d %I64d %I64d %I64d %I64d", &x, &y, &m, &n, &l);
if (m == n)
{
printf("Impossible\n");
return 0;
}
if (!linear_equation(n-m, l, x-y, ans, tmp))//(n-m) * ans + l * tmp = x-y
printf("Impossible\n");
else
printf("%I64d\n", ans);
return 0;
}