念心卓


私信TA

用户名:uq_80412005216

访问量:2311

签 名:

人不自渡,天也难助

等  级
排  名 1550
经  验 2700
参赛次数 2
文章发表 24
年  龄 0
在职情况 学生
学  校 哔哩哔哩大学
专  业 软件工程

  自我简介:

解题思路:
拥有基本的快速类思想
注意事项:
注意大数类的使用方法
参考代码:

    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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区