http://acm.nefu.edu.cn/test/problemshow.php?problem_id=84
Time Limit 1000ms
Memory Limit 65536K
description |
|
input |
|
output |
|
sample_input |
|
sample_output |
|
同 这道题。
完整代码:
/*2ms,700KB*/
#include<cstdio>
using namespace std;
void extend_gcd(long long a, long long b, long long &x, long long &y, long long &gcd)
{
if (b == 0)
{
x = 1;
y = 0;
gcd = a;
}
else
{
extend_gcd(b, a % b, x, y, gcd);
int temp = x;
x = y;
y = temp - a / b * y;
}
}
long long gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
long long n, d, x, y;
int t;
scanf("%d", &t);
while (t--)
{
scanf("%lld%lld%lld%lld", &n, &d, &x, &y);
long long a = d;
long long b = n;
long long c = y - x;
long long s = gcd(a, b);
if (c % s)
printf("Impossible\n");
else
{
long long x0, y0, q;
a /= s;
b /= s;
c /= s;
extend_gcd(a, b, x0, y0, q);
x0 *= c;
if (b < 0)
b = -b;
x0 = (x0 % b + b) % b;
printf("%lld\n", x0);
}
}
}