import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
long p = input.nextLong();
System.out.println(qmi(a,b,p));
}
public static int qmi(int a,int b,long p){
long res=1%p;long k=a;
while(b!=0){
if((b&1)==1){
res=res*k%p;
}
b=b>>1;
k*=k;
k%=p; //这里不取余好像有的数字不给过,还没相通为什么
}
return (int) res;
}
}
算法中快速幂问题
快速幂算法Java实现
本文介绍了一种使用Java实现的快速幂算法,该算法通过循环而非递归的方式进行计算,提高了计算效率。代码中详细展示了如何接收用户输入,并计算特定整数的幂次运算结果,同时确保了计算过程中数值不会过大而溢出。

被折叠的 条评论
为什么被折叠?



