题目
扩展欧几里得的应用,先将公式变形
同时将结果扩大res/b
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static int n;
public static void main(String[] args) throws IOException {
String[] s = br.readLine().split(" ");
n = Integer.parseInt(s[0]);
while (n-- > 0) {
int x[] = new int[1];
int y[] = new int[1];
s = br.readLine().split(" ");
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
int m = Integer.parseInt(s[2]);
int res = exgcd(a, m, x, y);
if (b % res != 0) pw.println("impossible");
else pw.println((long) x[0] * (b / res) % m);
}
pw.flush();
br.close();
}
private static int exgcd(int a, int b, int[] x, int[] y) {
if (b == 0) {
x[0] = 1;
y[0] = 0;
return a;
}
int res = exgcd(b, a % b, y, x);
y[0] = y[0] - a / b * x[0];
return res;
}
}