-
题目
求 a 的 b 次方对 p 取模的值。 -
输入格式
三个整数 a,b,p ,在同一行用空格隔开。 -
输出格式
输出一个整数,表示a^b mod p
的值。 -
数据范围
0≤a,b≤10^9
1≤p≤10^9 -
输入样例
3 2 7
-
输出样例
2
-
代码1
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int p = sc.nextInt(); long res = 1 % p; long k = a; while(b > 0) { if((b & 1) == 1) { res = res * k % p; } k = k * k % p; b >>= 1; } System.out.println(res); } }
AcWing0089. a^b
最新推荐文章于 2024-09-12 19:02:20 发布