题目描述
分析
首先是扩展欧几里得算法的知识
然后是关于这个题的分析
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b, ll &x, ll &y)
{
if (!b)
{
x = 1, y = 0;
return a;
}
ll d = gcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
int main()
{
int t;
cin >> t;
while (t--)
{
ll n, d, x, y, a, b;
cin >> n >> d >> x >> y;
ll g = gcd(n, d, a, b);
if ((y - x) % g)
cout << "Impossible" << endl;
else
{
b *= (y - x) / g;
n /= g;
ll res = (b % n + n) % n;
cout << res << endl;
}
}
return 0;
}