题目
输入输出
样例
解题思路
1、 题目非常简单,这道题就是考二分法快速幂运算,因为这个题运算出来的数会非常大,而且编译时间长,所以不能用直接运算的方法。
2、 二分法详解
3、 每次把指数缩小成原来的一半,底数增大到原来的平方,判断指数的奇偶性,奇数就减一继续重复,这里就需要把刚才底数乘的数都乘起来,注意没一步都需要对要求的数取余,这样才不会使数值过大。这里用long,int过不去OJ系统。
参考代码
package Test;
import java.util.Scanner;
public class Test2f {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
long sum=1;
long x=sc.nextLong();
long h=sc.nextLong();
long m=sc.nextLong();
while(h>0) {
if(h%2==0) {
x=x*x%m;
h=h/2;
}else {
h=h-1;
sum=sum*x%m;
h=h/2;
x=x*x%m;
}
}
System.out.println(sum);
}
}
}