Given four numbers A, B, C and M, where M is prime number. Our task is to find A^{BC} (mod M).

Input : A = 2, B = 4, C = 3, M = 23 Output : 6 2^{43}(mod 23) = 6

A **Naive Approach **is to calculate res = B^{C} and then calculate A^{res} % M by modular exponential. The problem of this approach is that we can’t apply directly mod M on B^{C}, so we have to calculate this value without mod M. But if we solve it directly then we will come up with the large value of exponent of A which will definitely overflow in final answer.

An **Efficient approach** is to reduce the B^{C} to a smaller value by using the Fermat’s Little Theorem, and then apply Modular exponential.

According the Fermat's littlea^{(M - 1)}= 1 (mod M) if M is a prime. So if we rewrite B^{C}as x*(M-1) + y, then the task of computing A^{BC}becomes A^{x*(M-1) + y}which can be written as A^{x*(M-1)}*A^{yx*(M-1)}= 1. So task of computing A^{BC}reduces to computing A^{y}What is the value of y?From B^{C}= x * (M - 1) + y, y can be written as B^{C}% (M-1)We can easily use the above theorem such that we can getA ^ (B ^ C) % M = (A ^ y ) % MNow we only need to find two things as:-1. y = (B ^ C) % (M - 1) 2. Ans = (A ^ y) % M

## C++

// C++ program to find (a^b) mod m for a large 'a' #include<bits/stdc++.h> using namespace std; // Iterative Function to calculate (x^y)%p in O(log y) unsigned int power(unsigned int x, unsigned int y, unsigned int p) { unsigned int res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res*x) % p; // y must be even now y = y>>1; // y = y/2 x = (x*x) % p; } return res; } unsigned int Calculate(unsigned int A, unsigned int B, unsigned int C, unsigned int M) { unsigned int res, ans; // Calculate B ^ C (mod M - 1) res = power(B, C, M-1); // Calculate A ^ res ( mod M ) ans = power(A, res, M); return ans; } // Driver program to run the case int main() { // M must be be a Prime Number unsigned int A = 3, B = 9, C = 4, M = 19; cout << Calculate(A, B, C, M); return 0; }

## Python

# Python program to calculate the ans def calculate(A, B, C, M): # Calculate B ^ C (mod M - 1) res = pow(B, C, M-1) # Calculate A ^ res ( mod M ) ans = pow(A, res, M) return ans # Driver program to run the case A = 3 B = 9 C = 4 # M must be Prime Number M = 19 print( calculate(A, B, C, M) )

Output: 18

**Time Complexity: **O(log(B) + log(C))

**Auxiliary space: **O(1)

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.