Lucas Theorem / Large Binomial Mod Prime¶
This lane starts when a binomial coefficient problem stops being:
precompute factorials once, answer nCk mod p in O(1)
and becomes:
n is too large relative to p, so one factorial table no longer covers the query
The key shift is:
- stop treating
nandkas one huge pair - write them in base
p - reduce the large query into many tiny digit-level binomials
For contest work, this is the exact route for:
C(n, k) mod pwithpprime- some queries where
n >= p, so ordinaryfact / inv_facttables stop being enough - special cases like parity of binomial coefficients when
p = 2
At A Glance¶
- Use when: the task is really
nCk mod pfor a prime modulus, andncan be too large for one factorial table belowp - Core worldview: write
nandkin basep, then multiply small digit-level binomials - Main tools: Lucas theorem, factorial / inverse-factorial table for
0..p-1, Fermat inverse - Typical complexity:
O(p)precompute, thenO(log_p n)per query - Main trap: reaching for Lucas when
pis huge, soO(p)precompute is not actually contest-safe
Strong contest signals:
- many
nCk mod pqueries under one fixed prime modulus - at least some queries cross the boundary
n >= p - the statement is still about one prime modulus, not a composite reconstruction
- parity or small-prime residue patterns in Pascal-like objects matter
Strong anti-cues:
- every query satisfies
max_n < p, so one ordinary factorial table is enough - the modulus is composite, so CRT / generalized Lucas would be needed instead
- the real problem is counting-model setup, not binomial evaluation itself
pis so large thatO(p)precompute is already a non-starter
Prerequisites¶
- Modular Arithmetic
- Counting Basics if the combinatorial meaning of
nCkis still shaky
Helpful nearby anchors:
- Chinese Remainder / Linear Congruences as a future compare point for composite-mod extensions
- factorial-binomial-mod.cpp
- Distributing Apples
Problem Model And Notation¶
We want:
where:
pis primenandkmay be much larger thanp
Write the base-p expansions:
with:
Lucas theorem says:
So the large query is reduced to many small binomial queries where the arguments are below p.
From Brute Force To The Right Idea¶
Brute Force¶
The ordinary contest route for one prime modulus is:
- precompute
fact[i]andinv_fact[i]up to somemax_n - answer:
in O(1) time
That is excellent when:
and the precompute limit is small enough.
Where It Breaks¶
If:
then n! mod p is already 0, because p appears as a factor.
So the ordinary factorial-table formula no longer behaves the way it did for n < p.
That is the real boundary.
The problem is not:
- "how do I optimize factorials more?"
It is:
- "how do I rewrite the large binomial in a way that respects base-
pdigit structure?"
Structural Observation¶
The prime modulus p does not only control inversion.
It also controls the digit system that breaks the problem apart.
Once n and k are expressed in base p, Lucas theorem turns one huge binomial into a product of tiny ones.
That is why the theorem belongs in this repo as its own lane, not just as a footnote under modular arithmetic.
Core Invariant And Why It Works¶
1. Digit Decomposition Is The Real State¶
The large pair (n, k) is processed digit by digit:
current answer = product of all processed base-p digit binomials
At each step:
ni = n % pki = k % p- multiply by
C(ni, ki) mod p - divide both
nandkbyp
That loop is the whole computational invariant.
2. Small Binomials Are Ordinary Again¶
Each digit pair satisfies:
So the subproblem:
is back in the easy regime.
That is exactly where one factorial / inverse-factorial table for:
becomes useful.
3. Why Zero Appears Early¶
If for some digit:
then:
and therefore the entire product is 0 mod p.
This is one of the most useful implementation payoffs:
one impossible digit is enough to kill the whole answer
4. Boundary With Ordinary Factorial Tables¶
If every query satisfies:
then Lucas adds no value.
The lighter route is still:
- precompute to
max_n - answer with one factorial formula
That is why the right chooser is:
- ordinary table first when
max_n < p - Lucas when the query range crosses
p
Variant Chooser¶
Use Ordinary Prime-Mod Binomial Precompute When¶
- the modulus
pis prime - all queries satisfy
max_n < p - one table up to
max_nis cheap
That is the route in:
Use Lucas Theorem When¶
- the modulus is still one prime
p - some queries have
n >= p pis small enough thatO(p)precompute is acceptable
That is this lane.
Do Not Use Lucas For Composite Modulus¶
If the modulus is composite, the prime-mod digit theorem is no longer enough.
That becomes a future lane involving:
- generalized Lucas
- prime powers
- CRT reconstruction
This repo is not shipping that extension in this page.
Worked Examples¶
Example 1. Tiny Lucas Merge¶
Compute:
Write numbers in base 5:
Lucas gives:
But:
so:
The important lesson is not the number 0.
It is:
- one impossible digit kills the whole product immediately
Example 2. Query Range Boundary¶
Suppose the modulus is:
and the queries are:
Then max_n < p, so Lucas is unnecessary.
The clean route is one factorial table up to 12.
But if one query becomes:
then the ordinary single table no longer explains the large-n jump.
That is the point where Lucas becomes the exact lane.
Example 3. Library Checker Binomial Coefficient (Prime Mod)¶
The clean reusable split is:
- read all queries under one prime modulus
p - if every query has
n < p, use one ordinary factorial table tomax_n - otherwise, if
pis still precompute-safe, build0..p-1and answer with Lucas digit decomposition
That is why this problem is such a good repo anchor:
- it teaches the chooser, not just the theorem
Pseudocode¶
build fact[0..p-1], inv_fact[0..p-1]
small_binom(n, k):
if k < 0 or k > n:
return 0
return fact[n] * inv_fact[k] * inv_fact[n-k] mod p
lucas(n, k):
ans = 1
while n > 0 or k > 0:
ni = n % p
ki = k % p
if ki > ni:
return 0
ans = ans * small_binom(ni, ki) mod p
n /= p
k /= p
return ans
Implementation Notes¶
- Lucas needs
pprime. - The starter in this repo assumes:
pis small enough forO(p)precompute- one digit table
0..p-1is feasible - If
pis huge but every query still hasn < p, ordinary factorial precompute tomax_nis usually cleaner. - Use
long longfornandk, even if the digit table is indexed byint. - Return
0immediately on the first digit whereki > ni.
Worked Compare Points In This Repo¶
- Distributing Apples: ordinary factorial-table binomial route under a large prime modulus
- Chinese Remainder / Linear Congruences: future compare point when the modulus stops being prime
- Modular Arithmetic: inverse discipline and prime-mod arithmetic layer
In This Repo¶
- Lucas Theorem / Large Binomial Mod Prime ladder
- Lucas Theorem hot sheet
- Binomial Coefficient (Prime Mod) note
- starter template
- compare points:
- Modular Arithmetic
- Chinese Remainder / Linear Congruences
References¶
- Reference: cp-algorithms - Binomial Coefficients
- Essay / Blog: AtCoder ABC251 editorial (Lucas theorem section)
- Practice: Library Checker - Binomial Coefficient (Prime Mod)
- Practice: Kattis - Odd Binomial Coefficients