Java实现 蓝桥杯VIP 算法提高 快速幂

算法提高 快速幂
时间限制:1.0s 内存限制:256.0MB
问题描述
  给定A, B, P,求(A^B) mod P。
输入格式
  输入共一行。
  第一行有三个数,N, M, P。
输出格式
  输出共一行,表示所求。
样例输入
2 5 3
样例输出
2
数据规模和约定
  共10组数据
  对100%的数据,A, B为long long范围内的非负整数,P为int内的非负整数。

import java.util.Scanner;


public class 快速幂 {
static Scanner in = new Scanner(System.in);
	
	static long QucikMod (long a, long b, long mod) {
		long res = 1;
		a %= mod;
		while(b != 0) {
			if(b%2 == 1)
				res = res * a % mod;
			a = a * a % mod;
			b /= 2;
		}
		return res;
	}

	public static void main(String[] args) {
		
		long a = in.nextLong(), b = in.nextLong(), c = in.nextLong();
		
		System.out.println(QucikMod(a, b, c));
		

	}


}

原文地址:https://www.cnblogs.com/a1439775520/p/13078420.html