博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用模重复平方法求b^n mod m
阅读量:4697 次
发布时间:2019-06-09

本文共 986 字,大约阅读时间需要 3 分钟。

package 模重复平方法;import java.math.BigInteger;import java.util.Scanner;/* * 要求: * 1编程实现模重复平方计算法 */public class Ah {	public static void main(String[] args) {		BigInteger b,m,n;		Scanner sc=new Scanner(System.in);		System.out.println("请输入b");		b=sc.nextBigInteger();		System.out.println("请输入n");		n=sc.nextBigInteger();		System.out.println("请输入m");		BigInteger a=new BigInteger("1");	    m=sc.nextBigInteger();		String t1=n.toString(2);		int[] N=new int[t1.length()];		for(int i=0;i<=t1.length()-1;i++)		{			N[i]=Integer.parseInt(t1.substring(t1.length()-i-1,t1.length()-i));		}				for(int j=0;j<=t1.length()-1;j++)		{						if(j!=t1.length()-1)			{				if(N[j]==1)			       a=(a.multiply(b)).remainder(m);			    if(N[j]==0)				   a=a.remainder(m);			    b=(b.multiply(b)).remainder(m);			}			else			{				if(N[j]==1)				    a=(a.multiply(b)).remainder(m);				 if(N[j]==0)					a=a.remainder(m);			}						}		System.out.println(a);//测试							}}

转载于:https://www.cnblogs.com/iamjuruo/p/7470929.html

你可能感兴趣的文章
CSHARP-349
查看>>
基数排序
查看>>
跟我学android-Android应用基本组件介绍(五)
查看>>
UIAlertView
查看>>
通过Instant Client包来使用SQL*PLUS
查看>>
ArcGIS:ArcToolBox工具使用——提取DEM/DSM中的高程点
查看>>
Dwarf Tower
查看>>
pat 1074. 宇宙无敌加法器(20)
查看>>
2.Linux环境下配置Solr4.10.3
查看>>
sqlServer 获取最新的一条数据
查看>>
day29
查看>>
Java Web ServletResponse
查看>>
HTML静态网页--JavaScript-语法
查看>>
es 的crud
查看>>
视频采集以及播放的流程
查看>>
mapreduce排序比较器的选取
查看>>
“饮水机”:形象比喻 深入浅出理解RAID
查看>>
Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法[转]
查看>>
ASP.NET MVC RedirectToRoute类[转]
查看>>
编码理解
查看>>