题解 | #快速乘#
快速乘
https://www.nowcoder.com/practice/043c66e95fe548d0b8e56c1830330f93
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
//和快速幂一样的思路
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; i++) {
String[] lines = in.readLine().split(" ");
int a = Integer.parseInt(lines[0]);
int b = Integer.parseInt(lines[1]);
int p = Integer.parseInt(lines[2]);
int res = 0;
while (b > 0) {
if (b % 2 == 1) {
res += a;
res %= p;
b--;
}
b /= 2;
a += a;
a %= p;
}
out.write(res + "\n");
}
in.close();
out.close();
}
}

查看22道真题和解析