The Wayback Machine - https://web.archive.org/web/20210813152716/https://www.geeksforgeeks.org/find-remainder-when-a-number-a-raised-to-n-factorial-is-divided-by-p/
Skip to content
Related Articles

Related Articles

Improve Article

Find remainder when a number A raised to N factorial is divided by P

  • Last Updated : 13 Aug, 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++




// C++ program for above approach
#include <bits/stdc++.h>
  
using namespace std;
  
// Function to calculate factorial of a Number
long long int fact(long long int n)
{
    long long int ans = 1;
  
    // Calculating factorial
    for (long long int i = 2; i <= n; i++)
        ans *= i;
  
    // Returning factorial
    return ans;
}
  
// Function to calculate resultant remainder
long long int remainder(
    long long int n,
    long long int a,
    long long int p)
{
  
    // Function call to calculate
    // factorial of n
    long long int len = fact(n);
    long long int ans = 1;
  
    // Calculating remainder
    for (long long int i = 1; i <= len; i++)
        ans = (ans * a) % p;
  
    // Returning resultant remainder
    return ans;
}
  
// Driver Code
int main()
{
    // Given Input
    long long int A = 2, N = 1, P = 2;
  
    // Function Call
    cout << remainder(N, A, P) << endl;
}
Output:
0

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:

  1. x^(a*b*c) can be written as :- (((x^a)^b)^c) …property of power.
  2. ((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++




// C++ program for above approach
#include <bits/stdc++.h>
  
using namespace std;
  
// Function to calculate
// (A^N!)%P in O(log y)
long long int power(
    long long x,
    long long int y,
    long long int p)
{
    // Initialize result
    long long int res = 1;
  
    // Update x if it is more than or
    // Equal to p
    x = x % p;
  
    // In case x is divisible by p;
    if (x == 0)
        return 0;
  
    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;
        x = (x * x) % p;
    }
  
    // Returning modular power
    return res;
}
  
// Function to calculate resultant remainder
long long int remainder(
    long long int n,
    long long int a,
    long long int p)
{
  
    // Initializing ans
    to store final remainder long long int ans = a % p;
  
    // Calculating remainder
    for (long long int i = 1; i <= n; i++)
        ans = power(ans, i, p);
  
    // Returning resultant remainder
    return ans;
}
  
// Driver Code
int main()
{
    // Given Input
    long long int A = 2, N = 1, P = 2;
  
    // Function Call
    cout << remainder(N, A, P) << endl;
}
Output:
0

Time complexity: O(N*logN)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :