题目链接:青蛙约会
AC代码:
import java.util.Scanner;
public class Main {
static long x;
static long y;
//返回值为最大公约数
static long ext_gcd(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long res = ext_gcd(b, a % b);
long x1 = x;
x = y;
y = x1 - a / b * y;
return res;
}
//返回值为最大公约数
static long linearEquation(long a, long b, long m) throws Exception {
long d = ext_gcd(a, b);
if (m % d != 0) {
throw new Exception("无解");
}
long n = m / d;
x *= n;
y *= n;
return d;
}
static int gcd(int m, int n) {
return n == 0 ? m : gcd(n, m % n);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long x1 = in.nextInt();
long y2 = in.nextInt();
long m = in.nextInt();
long n = in.nextInt();
long L = in.nextInt();
long a = m - n;
long b = L;
m = y2 - x1;
long d = 0;
try {
d = linearEquation(a, b, m);
long x0 = x;//有可能小于零
b /= d;
b = Math.abs(b);
x0 = (x0 % b + b) % b;
System.out.println(x0);
} catch (Exception e) {
System.out.println("Impossible");
}
}
}