牛客小白月赛12 B 华华教月月做数学(Java大数快速幂模板)

链接:https://ac.nowcoder.com/acm/contest/392/B

题意:给一个T,下面T行,每行给出A、B、P,求A^{B} modP

思路:Java大数快速幂模板。

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int t;
		BigInteger a,b,mod,ans;
		Scanner cin = new Scanner(System.in);
		t=cin.nextInt();
		while(t-->0){
			
			a=cin.nextBigInteger();
			b=cin.nextBigInteger();
			mod=cin.nextBigInteger();
			ans=poww(a,b,mod);
			System.out.println(ans.toString());
		}
	}
	public static BigInteger poww(BigInteger a,BigInteger b,BigInteger mod){
		BigInteger res=BigInteger.ONE;
		int c=2,aa=1;
		BigInteger d = BigInteger.valueOf(c);
		while(b.compareTo(BigInteger.ZERO)!=0){
			if(b.mod(d).compareTo(BigInteger.ONE)==0){
				res=res.multiply(a);
				res=res.mod(mod);
			}
			a=a.multiply(a);
			a=a.mod(mod);
			b=b.divide(d);
		}
		return res;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值