import java.math.BigInteger; import java.util.*; public class LANQIAO1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); BigInteger x, y, p; for (int i = 0; i < t; i++) { x = sc.nextBigInteger(); y = sc.nextBigInteger(); p = sc.nextBigInteger(); System.out.println(f(x, y, p)); } } public static BigInteger f(BigInteger x, BigInteger y,BigInteger p){ // 保险起见 返回 1 % p if(y == BigInteger.valueOf(0) ) return BigInteger.valueOf(1).mod(p); if(y.mod(BigInteger.valueOf(2)) == BigInteger.valueOf(0)) { // 定义一个变量让他只调用1次函数 tmp自己×自己 结果取模 BigInteger tmp = new BigInteger(String.valueOf(f(x, y.divide(BigInteger.valueOf(2)),p))); return tmp.multiply(tmp).mod(p); }else { BigInteger tmp = new BigInteger(String.valueOf(f(x, y.divide(BigInteger.valueOf(2)),p))); return tmp.multiply(tmp).mod(p).multiply(x).mod(p); } } }
蓝桥杯真题-快速幂
最新推荐文章于 2024-09-16 23:08:22 发布