Given three numbers n, r and p, compute value of nCr mod p. Here p is a prime number greater than n. Here nCr is Binomial Coefficient.
Input: n = 10, r = 2, p = 13 Output: 6 Explanation: 10C2 is 45 and 45 % 13 is 6. Input: n = 6, r = 2, p = 13 Output: 2
We have discussed following methods in previous posts.
Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
Compute nCr % p | Set 2 (Lucas Theorem)
In this post, Fermat Theorem based solution is discussed.
Fermat’s little theorem and modular inverse
Fermat’s little theorem states that if p is a prime number, then for any integer a, the number ap – a is an integer multiple of p. In the notation of modular arithmetic, this is expressed as:
ap = a (mod p)
For example, if a = 2 and p = 7, 27 = 128, and 128 – 2 = 7 × 18 is an integer multiple of 7.
If a is not divisible by p, Fermat’s little theorem is equivalent to the statement a p – 1 – 1 is an integer multiple of p, i.e
ap-1 = 1 (mod p)
If we multiply both sides by a-1, we get.
ap-2 = a-1 (mod p)
So we can find modular inverse as p-2.
We know the formula for nCr nCr = fact(n) / (fact(r) x fact(n-r)) Here fact() means factorial. nCr % p = (fac[n]* modIverse(fac[r]) % p * modIverse(fac[n-r]) % p) % p; Here modIverse() means modular inverse under modulo p.
Following is the implementation of the above algorithm. In the following implementation, an array fac is used to store all the computed factorial values.
Value of nCr % p is 6
In competitive programming, we can pre-compute fac for given upper limit so that we don’t have to compute it for every test case. We also can use unsigned long long int everywhere to avoid overflows.
This article is contributed by Nikhil Papisetty. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Fermat's little theorem
- Compute nCr % p | Set 2 (Lucas Theorem)
- Fermat's Last Theorem
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Primality Test | Set 2 (Fermat Method)
- Calculate nCr using Pascal's Triangle
- Fermat's Factorization method for large numbers
- Check if a number is Fermat Pseudoprime
- Program to find first N Fermat Numbers
- Fermat's Factorization Method
- Program to calculate value of nCr
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Program to calculate the value of nCr Efficiently
- Find a pair from the given array with maximum nCr value
- Find if nCr is divisible by the given prime
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Queries of nCr%p in O(1) time complexity
- Using Chinese Remainder Theorem to Combine Modular equations
- Chinese Remainder Theorem | Set 1 (Introduction)
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)