链接:https://ac.nowcoder.com/acm/contest/392/B
题意:给一个T,下面T行,每行给出A、B、P,求。
思路:Java大数快速幂模板。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int t;
BigInteger a,b,mod,ans;
Scanner cin = new Scanner(System.in);
t=cin.nextInt();
while(t-->0){
a=cin.nextBigInteger();
b=cin.nextBigInteger();
mod=cin.nextBigInteger();
ans=poww(a,b,mod);
System.out.println(ans.toString());
}
}
public static BigInteger poww(BigInteger a,BigInteger b,BigInteger mod){
BigInteger res=BigInteger.ONE;
int c=2,aa=1;
BigInteger d = BigInteger.valueOf(c);
while(b.compareTo(BigInteger.ZERO)!=0){
if(b.mod(d).compareTo(BigInteger.ONE)==0){
res=res.multiply(a);
res=res.mod(mod);
}
a=a.multiply(a);
a=a.mod(mod);
b=b.divide(d);
}
return res;
}
}