Given three numbers n, r and p, compute value of nCr mod p.
Input: n = 10, r = 2, p = 13 Output: 6 Explanation: 10C2 is 45 and 45 % 13 is 6. Input: n = 1000, r = 900, p = 13 Output: 8
We strongly recommend to refer below post as a prerequisite of this.
We have introduced overflow problem and discussed Dynamic Programming based solution in above set 1. The time complexity of the DP based solution is O(n*r) and it required O(n) space. The time taken and extra space become very high for large values of n, especially values close to 109.
In this post, Lucas Theorem based solution is discussed. Time complexity of this solution is O(p2 * Logp n) and it requires only O(p) space.
For non negative integers n and r and a prime p, the following congruence relation holds:
Using Lucas Theorem for nCr % p:
Lucas theorem basically suggests that the value of nCr can be computed by multiplying results of niCri where ni and ri are individual same-positioned digits in base p representations of n and r respectively..
The idea is to one by one compute niCri for individual digits ni and ri in base p. We can compute these values DP based solution discussed in previous post. Since these digits are in base p, we would never need more than O(p) space and time complexity of these individual computations would be bounded by O(p2).
Below is implementation of above idea
Value of nCr % p is 8
Time Complexity: Time complexity of this solution is O(p2 * Logp n). There are O(Logp n) digits in base p representation of n. Each of these digits is smaller than p, therefore, computations for individual digits take O(p2). Note that these computations are done using DP method which takes O(n*r) time.
Alternate Implementation with O(p2 + Logp n) time and O(p2) space:
The idea is to precompute Pascal triangle for size p x p and store it in 2D array. All values needed would now take O(1) time. Therefore overall time complexity becomes O(p2 + Logp n).
This article is contributed by Ruchir Garg. 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.
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Primality Test | Set 5(Using Lucas-Lehmer Series)
- Lucas Numbers
- Longest sub-sequence of array containing Lucas numbers
- Sum of numbers from 1 to N which are in Lucas Sequence
- Lucas Primality Test
- Program to calculate value of nCr
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Queries of nCr%p in O(1) time complexity
- Program to calculate the value of nCr Efficiently
- Calculate nCr using Pascal's Triangle
- 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
- Chinese Remainder Theorem | Set 1 (Introduction)
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Wilson's Theorem
- Zeckendorf's Theorem (Non-Neighbouring Fibonacci Representation)