给出3个正整数A B C,求A^B Mod C。
例如,3 5 8,3^5 Mod 8 = 3。
Input
3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)
Output
输出计算结果
Input示例
3 5 8
Output示例
3
题解:
import java.util.Scanner;
import 算法模板.快速幂模板;
public class M1046 {
public static void main(String[] args) {
//快速幂公式
//a^b mod c = (a mod c)^b mod c
Scanner in = new Scanner(System.in);
long a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
long cj = 1;
a = a%c;//先缩小范围,怕范围溢出
while(b!=0) {
if(b%2==1)//奇数--且肯定有一时候到1
cj = (cj * a)%c;
a = (a*a)%c;
b = b/2;
}
System.out.println(cj);
}
}