题解 | #快速乘#
快速乘
https://www.nowcoder.com/practice/043c66e95fe548d0b8e56c1830330f93
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int q = in.nextInt();
for(int i =0;i<q;i++){
long a = in.nextInt();
long b = in.nextInt();
long p = in.nextInt();
System.out.println(quickm(a,b,p));
}
}
public static long quickm(long a,long b,long p){
long ans = 0;
while(b > 0){
if(b%2==1){
b-=1;
ans = (ans + a)%p;
}
b/=2;
a = (a+a)%p;
}
return ans;
}
}
查看9道真题和解析