高精度快速幂(Java版)
2024-08-25 17:21:11
import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*; public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger a, b, m, ans;
while (cin.hasNext()) {
ans = new BigInteger("1");
a = cin.nextBigInteger();
b = cin.nextBigInteger();
m = cin.nextBigInteger();
a = a.mod(m);
while (b.compareTo(new BigInteger("0")) > 0) {
if (b.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE) == 0) // if(n%2==1)
ans = ans.multiply(a).mod(m); // sq=(sq*p)%m;
a = a.multiply(a).mod(m); // p=(p*p)%m;
b = b.divide(BigInteger.valueOf(2));
}
System.out.println(ans);
}
}
}
最新文章
- MsSQLserver中修改字段值系统自动生成的脚本
- 张艾迪(创始人): 整合全新的UIW.AD概念模式
- webdriver无法打开firefox
- PHP面向对象04_串行化
- 安装mac os x时about a second remaining解决方法
- 浅谈Java回调机制
- 【ruby】ruby基础知识
- 删除Ngnix 日志
- Flume简介与使用(二)——Thrift Source采集数据
- web api 开发之 filter
- Buffett saying
- Hive 行列转换
- iPhone 屏幕分辨率
- Android开发的一些总结
- Linux命令--tree
- HDU1060
- wireless Penetration Testing &; Honeypot and Mis-Association attacks
- SpringBoot使用thymeleaf模板引擎
- matlab中hold on 和hold off功能的区别
- (九)ROS安装rviz模拟器