1. 快速幂解决a^b问题
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问题
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);
}
}
}