解题思路:
需要注意,计算过程中数据量最大值超过了int类型所能表示的最大值,因此应该用long表示
解题代码:
import java.io.*;
public class Main {
static long qmi(int a,int k,int p){
long res = 1;
long al = a; //采用long数据类型避免计算过程中数据溢出
while ( k > 0){
if ((k & 1) == 1) res = res * al % p;
k >>= 1;
al = al * al % p;
}
return res;
}
public static void main(String[] args) throws IOException {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader in = new BufferedReader((new InputStreamReader(System.in)));
int n = Integer.parseInt(in.readLine());
while (n -- > 0){
int a,k,p;
String[] str = in.readLine().split(" ");
a = Integer.parseInt(str[0]);k = Integer.parseInt(str[1]); p = Integer.parseInt(str[2]);
out.write(qmi(a,k,p)+"");out.newLine();
}
out.flush();
}
}