AcWing 89. 【java】a^b
原题链接
简单
作者:
tt2767
,
2019-12-01 21:24:28
,
所有人可见
,
阅读 765
// 又忘了 res = 1 % p
// 从 python 切回java 有点不适应啊, 一直报声明不对,没有; 😂
import java.io.*;
import java.util.*;
public class Main{
public static void main(String args[]) throws Exception{
Scanner jin = new Scanner(System.in);
long a = jin.nextInt();
long b = jin.nextInt();
long p = jin.nextInt();
long res = power(a, b, p);
System.out.println(res);
}
public static long power(long a, long b, long p){
long res = 1 % p ;
while(b != 0){
if ((b & 1) == 1){
res = (res * a) % p ;
}
a = (a * a) % p ;
b = b >> 1;
}
return res;
}
}