Find remainder when a number A raised to N factorial is divided by P
Last Updated :
02 Sep, 2021
Given three integers A, N and P, the task is to find (A^(N!)) % P.
Examples:
Input: A = 2, N = 1, P = 2
Output: 0
Explanation: As (2^(1!)) = 2
Therefore 2 % 2 will be 0.
Input: A = 3, N = 3, P = 2
Output: 1
Naive Approach: The simplest solution of this problem can be find out factorial of N say f, and now calculate A to power f say pow and find its remainder with P.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int fact( long long int n)
{
long long int ans = 1;
for ( long long int i = 2; i <= n; i++)
ans *= i;
return ans;
}
long long int remainder(
long long int n,
long long int a,
long long int p)
{
long long int len = fact(n);
long long int ans = 1;
for ( long long int i = 1; i <= len; i++)
ans = (ans * a) % p;
return ans;
}
int main()
{
long long int A = 2, N = 1, P = 2;
cout << remainder(N, A, P) << endl;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static int fact( int n)
{
int ans = 1 ;
for ( int i = 2 ; i <= n; i++)
ans *= i;
return ans;
}
static int remainder(
int n,
int a,
int p)
{
int len = fact(n);
int ans = 1 ;
for ( int i = 1 ; i <= len; i++)
ans = (ans * a) % p;
return ans;
}
public static void main(String args[])
{
int A = 2 , N = 1 , P = 2 ;
System.out.println(remainder(N, A, P));
}
}
|
Python3
def fact(n):
ans = 1
for i in range ( 2 ,n + 1 , 1 ):
ans * = i
return ans
def remainder(n, a, p):
len1 = fact(n)
ans = 1
for i in range ( 1 ,len1 + 1 , 1 ):
ans = (ans * a) % p
return ans
if __name__ = = '__main__' :
A = 2
N = 1
P = 2
print (remainder(N, A, P))
|
C#
using System;
public class GFG{
static int fact( int n)
{
int ans = 1;
for ( int i = 2; i <= n; i++)
ans *= i;
return ans;
}
static int remainder(
int n,
int a,
int p)
{
int len = fact(n);
int ans = 1;
for ( int i = 1; i <= len; i++)
ans = (ans * a) % p;
return ans;
}
public static void Main( string []args)
{
int A = 2, N = 1, P = 2;
Console.WriteLine(remainder(N, A, P));
}
}
|
Javascript
<script>
function fact(n) {
let ans = 1;
for (let i = 2; i <= n; i++) ans *= i;
return ans;
}
function remainder(n, a, p) {
let len = fact(n);
let ans = 1;
for (let i = 1; i <= len; i++) ans = (ans * a) % p;
return ans;
}
let A = 2,
N = 1,
P = 2;
document.write(remainder(N, A, P));
</script>
|
Time complexity: O(N!)
Auxiliary Space: O(1)
Efficient Approach: The above can be optimized using concept of modular exponentiation and some properties of modulo and powers:
- x^(a*b*c) can be written as :- (((x^a)^b)^c) …property of power.
- ((x^a)^b) % p can be written as :- ((x^a) %p)^b) % p …property of modulo.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int power(
long long x,
long long int y,
long long int p)
{
long long int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int remainder(
long long int n,
long long int a,
long long int p)
{
long long int ans = a % p;
for ( long long int i = 1; i <= n; i++)
ans = power(ans, i, p);
return ans;
}
int main()
{
long long int A = 2, N = 1, P = 2;
cout << remainder(N, A, P) << endl;
}
|
Java
import java.io.*;
class GFG {
static long power( long x, long y, long p)
{
long res = 1 ;
x = x % p;
if (x == 0 )
return 0 ;
while (y > 0 )
{
if ((y & 1 ) == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static long remainder( long n, long a, long p)
{
long ans = a % p;
for ( long i = 1 ; i <= n; i++)
ans = power(ans, i, p);
return ans;
}
public static void main(String[] args)
{
long A = 2 , N = 1 , P = 2 ;
System.out.println(remainder(N, A, P));
}
}
|
Python3
def power(x, y, p):
res = 1
x = x % p
if (x = = 0 ):
return 0
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def remainder(n, a, p):
ans = a % p
for i in range ( 1 ,n + 1 ):
ans = power(ans, i, p)
return ans
A = 2
N = 1
P = 2
print (remainder(N, A, P))
|
C#
using System;
class GFG {
static long power( long x, long y, long p)
{
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long remainder( long n, long a, long p)
{
long ans = a % p;
for ( long i = 1; i <= n; i++)
ans = power(ans, i, p);
return ans;
}
public static void Main(String[] args)
{
long A = 2, N = 1, P = 2;
Console.Write(remainder(N, A, P));
}
}
|
Javascript
<script>
function power(x, y, p)
{
let res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function remainder(n, a, p) {
let ans = a % p;
for (let i = 1; i <= n; i++)
ans = power(ans, i, p);
return ans;
}
let A = 2, N = 1, P = 2;
document.write(remainder(N, A, P) + "<br>" );
</script>
|
Time complexity: O(N*logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...