参考代码:
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); BigInteger a = scanner.nextBigInteger(); BigInteger b = scanner.nextBigInteger(); BigInteger p = scanner.nextBigInteger(); String res = a.modPow(b, p).toString();//BigInteger内置方法,模意义下取幂 // long a = scanner.nextLong(); // long b = scanner.nextLong(); // long p = scanner.nextLong(); // long res = modPow(a % p, b, p);//先对a进行一次取余 System.out.print(res); } // 递归 // public static long modPow(long a, long b, long p) // { if(b == 0) // return 1; // boolean isOdd = (b & 1) == 1; // long res = modPow(a, b >> 1, p); // if(isOdd) // return (res * res % p * a % p) % p; // else // return res * res % p; // } // 迭代方法 // public static long modPow(long a, long b, long p) // { long res = 1; // while(b > 0) // { if((b & 1) == 1) // res *= a; // a *= a; // res %= p; // a %= p; // b >>= 1; // } // return res; // } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1432 |
1157题解浏览:716 |
1048题解(读入回车问题)浏览:565 |
C语言程序设计教程(第三版)课后习题12.5 (C语言代码)浏览:766 |
C语言训练-自守数问题 (C语言代码)浏览:748 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:617 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:722 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:497 |
简单的a+b (C语言代码)浏览:467 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:667 |