解题思路:
拥有基本的快速类思想
注意事项:
注意大数类的使用方法
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); BigInteger A = scanner.nextBigInteger();//底数 BigInteger B = scanner.nextBigInteger();//指数 BigInteger P = scanner.nextBigInteger();//取模数 //A^B; BigInteger res = BigInteger.ONE;//用来存放结果;初始化为1 //BigInteger.ONE = 1;BigInteger.ZERO = 0;BigInteger.TEN = 10 while( B.compareTo(BigInteger.ZERO) != 0) {//B的值与0进行比较,返回的是boolean类型true为1,false为0 if(B.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO) != 0) {//B%2 != 0 判断指数B是不是偶数 res = (res.multiply(A)).mod(P);//如果不是偶数,把指数为奇数时分离出来的底数的一次方收集好 } A = A.multiply(A).mod(P);//底数变大成原来的平方 B = B.divide(BigInteger.valueOf(2));//指数变为原来的一半 } System.out.println(res); }
0.0分
0 人评分
简单的a+b (C语言代码)浏览:681 |
C语言训练-求函数值 (C语言代码)浏览:573 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:550 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:606 |
DNA (C语言代码)浏览:391 |
演讲大赛评分 (C语言代码)浏览:1629 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:568 |
拆分位数 (C语言代码)浏览:441 |
简单的a+b (C语言代码)浏览:547 |
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言描述之函数调用)浏览:796 |