1. 快速幂解决a^b问题
![在这里插入图片描述](https://img-blog.csdnimg.cn/6dbfdb06bba546d6950425ccacc6c251.png)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long a,b,p;
while(sc.hasNext()){
a=sc.nextInt();
b=sc.nextInt();
p=sc.nextInt();
long ans=1;
while(b!=0){
if(b%2==1){
ans=(ans*a)%p;
}
b>>=1;
a=(a*a)%p;
}
System.out.println(ans%p);
}
}
}
2. 快速幂解决a*b问题
![在这里插入图片描述](https://img-blog.csdnimg.cn/c771a90b558440cb916bdd1e2f761324.png)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
long a,b,p;
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
a=sc.nextLong();
b=sc.nextLong();
p=sc.nextLong();
long ans=0;
while(b!=0){
if(b%2==1){
ans=(ans+a)%p;
}
b>>=1;
a=(a+a)%p;
}
System.out.println(ans%p);
}
}
}