参考代码:
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语言训练-立方和不等式 (C语言代码)浏览:751 |
C语言程序设计教程(第三版)课后习题1.6 (C++代码)浏览:899 |
简单的a+b (C语言代码)浏览:599 |
【金明的预算方案】 (C++代码)浏览:940 |
C语言训练-自由落体问题 (C语言代码)浏览:615 |
简单的a+b (C语言代码)浏览:486 |
GC的苦恼 (C语言代码)浏览:621 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:672 |
1134题解(求分析)浏览:729 |
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:673 |