acwing-89. a^b

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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();

System.out.println(power(a, b, p));
}

public static long power(long a, long b, long p){
long ans = 1 % p;
while(b > 0){
if((b&1) == 1){
ans = (ans * a) % p;
}

a = (a * a) % p;
b = b >> 1;
}

return ans;
}
}