The Lucas theorem helps to find the result of the binomial coefficient(nCr) modulo p efficiently.
If we are given three positive integers, n, r, p (prime number), then according to Lucas Theorem:
where n = nkpk + nk-1pk-1 + nk-2pk-2 + … + n1p + n0 and c = ckpk + ck-1pk-1 + ck-2pk-2 + … + c1p1 + c0 is the expression which can be derived from the above formula for m and n. It says that (n, c) =0 if n is lesser than c.
And (nCc) will be divisible by p only if at least one of base p digits of n is greater than the corresponding base (which is p) digit of m.
It can also be used for finding the convex hull.
Image credit go to Bruce Torrence, “Lucas-Gauss Theorem”, no changes made to the image, license BY-NC-SA 3.0
In the above figure there are five locators which are present on the boundaries of the convex hull.
Let suppose p=2 and (n,c) is (10, 4) is even, if and only if at least one of the binary digits of c is greater than the corresponding binary digits. So (10,4) here is even, which is 210 because 10 = 10102 has a greater digit than 4 = 01002 . And in this, (2k-1,n) will always be odd.
Example:
Suppose we want to find the remainder when a binomial coefficient is divided by a prime number. Then:
Let N = 1000 and C=300 and p = 13 then
300 = 1(132) + 10(13) + 1(130)
1000 = 5(132) + 11(13) + 12
Using Lucas’ Theorem:
(1000,300) = (5,1) x (11,10) x (12,1) = 5 x 11 x 12 = 5 x (-2) (-1) = 10
So the remainder is 10
Input: n = 200, r = 198, p = 7
Output: 6
Explanation: C(200, 198) is 19900 and 19900 % 13 is 10 .
Input: n = 100, r = 90, p = 13
Output: 8
Explanation: C(1000, 998) is 4950 and 4950 % 13 is 10.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
import java.lang.*;
class Solution {
static int nCrModpLasTh(int n, int r, int p) {
// Base case
if (r == 0)
return 1;
int ni = n % p;
int ri = r % p;
return (nCrModpLasTh(n / p, r / p, p) * //recursively calling nCrModpLasTh
nCrLucasTh(ni, ri, p)) % p;
}
//this will return an integer which is remainder of nCr modulus p(prime num)
//passing n,r,p arguments where n < p and r < p
static int nCrLucasTh(int n, int r, int p) {
int[] arr = new int[r + 1];
arr[0] = 1; // first row of Pascal Triangle with default value
for (int i = 1; i <= n; i++) {
int min = Math.min(i, r);
for (int j = min; j > 0; j--)
// (n , j) = (n-1) arr j + (n-1)arr(j-1);
arr[j] = (arr[j] + arr[j - 1]) % p;
}
return arr[r];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Test Cases:");
int t = sc.nextInt();
//until t is 0
while (t > 0) {
System.out.println("\nEnter N:");
int n = sc.nextInt();
System.out.println("\nEnter r:");
int r = sc.nextInt();
System.out.println("\nEnter p(prime number):");
int p = sc.nextInt();
System.out.println(" \n \n Then C(n, r) % p is " + nCrModpLasTh(n, r, p));
t--;
}
}
}
Time Complexity: O(p2 * Logp N)
Explanation: There are O(LogpN) digits in base p. Each digit from these numbers will be smaller than p. So calculating each digit takes O(N*N). Hence, the complexity will be O(p2 * LogpN).
Code Snippet:
Suppose pn is a prime number and m1, n1, m, n and i are non negative numbers with m1, n1 lesser than i. Then, according to Lucas theorem:
This result is based on the proof of Jacobsthal. It is also proved that pn =2 and pn =3 if we replace above ((i-1)/3) by (i/2), and by ((i-1)/2).
Proof of the Lucas theorem is based on observation. Most common problems can be resolved by the direct implementation of the Lucas theorem. Suppose we want to find the remainder of the binomial coefficient when it is divided by the prime number.
This can be explained further using Pascal’s triangle.
Image credit go to Larry Riddle
https://nntdm.net/papers/nntdm-18/NNTDM-18-4-01-06.pdf
https://web.archive.org/web/20170202003812/http://www.dms.umontreal.ca/~andrew/PDF/BinCoeff.pdf