Find power of power under mod of a prime
Given four numbers A, B, C and M, where M is prime number. Our task is to find ABC (mod M).
Example:
Input : A = 2, B = 4, C = 3, M = 23
Output : 6
43 = 64 so,
2^64(mod 23) = 6
A Naive Approach is to calculate res = BC and then calculate Ares % M by modular exponential. The problem of this approach is that we can’t apply directly mod M on BC, 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 BC to a smaller value by using the Fermat’s Little Theorem, and then apply Modular exponential.
According the Fermat's little
a(M - 1) = 1 (mod M) if M is a prime.
So if we rewrite BC as x*(M-1) + y, then the
task of computing ABC becomes Ax*(M-1) + y
which can be written as Ax*(M-1)*Ay.
From Fermat's little theorem, we know Ax*(M-1) = 1.
So task of computing ABC reduces to computing Ay
What is the value of y?
From BC = x * (M - 1) + y,
y can be written as BC % (M-1)
We can easily use the above theorem such that we can get
A ^ (B ^ C) % M = (A ^ y ) % M
Now we only need to find two things as:-
1. y = (B ^ C) % (M - 1)
2. Ans = (A ^ y) % M
C++
#include<bits/stdc++.h>
using namespace std;
unsigned int power(unsigned int x, unsigned int y,
unsigned int p)
{
unsigned int res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
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;
res = power(B, C, M-1);
ans = power(A, res, M);
return ans;
}
int main()
{
unsigned int A = 3, B = 9, C = 4, M = 19;
cout << Calculate(A, B, C, M);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int power( int x, int y, int p) {
int res = 1 ;
x = x % p;
while (y > 0 ) {
if (y % 2 != 0 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int Calculate( int A, int B, int C, int M) {
int res, ans;
res = power(B, C, M - 1 );
ans = power(A, res, M);
return ans;
}
public static void main(String[] args) {
int A = 3 , B = 9 , C = 4 , M = 19 ;
System.out.print(Calculate(A, B, C, M));
}
}
|
Python3
def calculate(A, B, C, M):
res = pow (B, C, M - 1 )
ans = pow (A, res, M)
return ans
A = 3
B = 9
C = 4
M = 19
print ( calculate(A, B, C, M) )
|
C#
using System;
class GFG {
static int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y % 2 != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int Calculate( int A, int B,
int C, int M)
{
int res, ans;
res = power(B, C, M - 1);
ans = power(A, res, M);
return ans;
}
public static void Main()
{
int A = 3, B = 9, C = 4, M = 19;
Console.Write(Calculate(A, B, C, M));
}
}
|
PHP
<?php
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
function Calculate( $A , $B , $C , $M )
{
$res ; $ans ;
$res = power( $B , $C , $M - 1);
$ans = power( $A , $res , $M );
return $ans ;
}
$A = 3; $B = 9;
$C = 4; $M = 19;
echo Calculate( $A , $B ,
$C , $M );
?>
|
Javascript
<script>
function power(x, y, p) {
let res = 1;
x = x % p;
while (y > 0) {
if (y % 2 != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function Calculate(A, B, C, M) {
let res, ans;
res = power(B, C, M - 1);
ans = power(A, res, M);
return ans;
}
let A = 3, B = 9, C = 4, M = 19;
document.write(Calculate(A, B, C, M));
</script>
|
Output:
18
Time Complexity: O(log(B) + log(C))
Auxiliary space: O(1)
Last Updated :
18 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...