题目
逆元的求法,用到欧拉定理和费马小定理,然后用快速幂优化。
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);
public static void pow(int a, int k, int q) {
int res = 1;
while (k != 0) {
if ((k & 1) == 1) res = (int) ((long) res * a % q);
k >>= 1;
a = (int) ((long) a * a % q);
}
if (res == 0) pw.println("impossible");
else pw.println(res);
}
public static void main(String[] args) throws IOException {
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
while (n-- > 0) {
s = br.readLine().split(" ");
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
if (a % b == 0) {
pw.println("impossible");
continue;
}
pow(a, b - 2, b);
}
pw.flush();
br.close();
}
}