AcWing 89. a^b - Java
原题链接
简单
作者:
熊本熊本熊
,
2019-05-16 10:58:28
,
所有人可见
,
阅读 1950
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Main solution = new Main();
Scanner reader = new Scanner(System.in);
int a = reader.nextInt();
int b = reader.nextInt();
int p = reader.nextInt();
int ans = solution.a_power_b(a,b,p);
System.out.println(ans);
}
public int a_power_b(int a, int b, int p){
long ans = 1; long k = a;
while (b > 0){
if( (b&1) == 1){
ans *= k;
ans = ans%p;
}
k = k*k;
k = k%p;
b = b>>1;
}
ans = ans%p;
return (int)ans;
}
}