题目链接:A/B
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 long inverseElement(long a, long mo) throws Exception {
long d = linearEquation(a, mo, 1);
x = (x % mo + mo) % mo;//x要大于零
return d;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
long n;
long B;
while (T-- != 0) {
n = in.nextInt();
B = in.nextInt();
try {
inverseElement(B, 9973);
System.out.println(n * x % 9973);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}