Extended Lucas theorem

中文版本请见这里。

\newcommand{\stirlingI}[2]{\genfrac{[}{]}{0pt}{}{#1}{#2}} Lucas theorem is quite well known for compute (nm)(modp)\binom{n}{m} \pmod{p}:

Let pp be a prime, n=u1p+u2n = u_1 p + u_2,m=v2p+v2m = v_2 p + v_2 where 0≤u2,v2<p0 \leq u_2, v_2 < p, then we have

(nm)≡(u1v1)(u2v2)(modp).\binom{n}{m} \equiv \binom{u_1}{v_1} \binom{u_2}{v_2} \pmod{p}.

However, Lucas theorem only works for prime pp. For composites CRT can rescue us, but we still have to solve the hard case where pp is a prime power, which this post mainly discusses. The post borrows a lot from a post by prabowo (although I guess he also references min_25’s blogpost), while having its own novelties (mostly the upper bound).

Andrew Granville also has a paper on Extended Lucas theroem. However, there are too many cases in the paper and the overall time complexity is also high. I’d never write it again after I did it once.

Analysis on n!n!

Define E(n)=max⁡k{pk∣n!}E(n) = \max_k \{p^k | n!\} as the number of trailing 0’s of the base-pp representation of n!n!. The efficient algorithm for computing E(n)E(n) is trivial. Now we define

(n!)p=∏i=1,p∤ini,(n!)_p = \prod_{i=1, p \nmid i}^n i,

that is, the product of integers in [n][n] which are not multiples of pp. Now, we have

n!=pE(n)∏k=0(⌊n/pk⌋!)p,n! = p^{E(n)} \prod_{k=0} \left( \left\lfloor n/p^k \right\rfloor !\right)_p,

and our task is to compute (n!)p mod pe(n!)_p \bmod p^e.

Suppose n=up+vn = up + v where 0≤v<m0 \leq v < m. We may have two terms for (n!)p(n!)_p:

(n!)p=(∏i=0u−1∏j=1p−1(ip+j))(∏i=1v(up+j)).(n!)_p = \left( \prod_{i=0}^{u-1} \prod_{j=1}^{p-1} (ip + j) \right) \left( \prod_{i=1}^v (up + j) \right).

Note that both terms have a structure of ∏j=1r(ip+j)\prod_{j=1}^r (ip + j), which can be seen as a polynomial of pp. What’s more interesting is that we only care about the polynomial modulo pep^e. More precisely, notice that

∏i=0k−1(n+i)=∑i=0k\stirlingIkini,\prod_{i=0}^{k-1} (n + i) = \sum_{i=0}^k \stirlingI{k}{i} n^i,

where \stirlingIki\stirlingI{k}{i} is the Stirling number of the first kind satisfying

\stirlingIn+1k=n\stirlingInk+\stirlingInk−1,\stirlingI00=1,\stirlingI0n=\stirlingIn0=0.\stirlingI{n + 1}{k} = n \stirlingI{n}{k} + \stirlingI{n}{k-1}, \quad \stirlingI{0}{0} = 1, \stirlingI{0}{n} = \stirlingI{n}{0} = 0.

Therefore, we may represent (n!)p(n!)_p by

(n!)p=(∏i=0u−1∏j=1p−1(ip+j))(∏i=1v(up+j))=(∏i=0u−1∑k=1p\stirlingIpk(ip)k−1)(∑k=1v\stirlingIv+1k(up)k−1)≡(∏i=0u−1∑k=1e\stirlingIpk(ip)k−1)(∑k=1e\stirlingIv+1k(up)k−1)(modpe)=\stirlingIp1u(∏i=0u−1∑k=1e\stirlingIpk\stirlingIp1(ip)k−1)(∑k=1e\stirlingIv+1k(up)k−1).\begin{aligned} (n!)_p & = \left( \prod_{i=0}^{u-1} \prod_{j=1}^{p-1} (ip + j) \right) \left( \prod_{i=1}^v (up + j) \right) \\ & = \left( \prod_{i=0}^{u-1} \sum_{k=1}^{p} \stirlingI{p}{k} (ip)^{k-1} \right) \left( \sum_{k=1}^{v} \stirlingI{v+1}{k} (up)^{k-1} \right) \\ & \equiv \left( \prod_{i=0}^{u-1} \sum_{k=1}^{e} \stirlingI{p}{k} (ip)^{k-1} \right) \left( \sum_{k=1}^{e} \stirlingI{v+1}{k} (up)^{k-1} \right) \pmod{p^e} \\ & = \stirlingI{p}{1}^u \left( \prod_{i=0}^{u-1} \sum_{k=1}^{e} \frac{\stirlingI{p}{k}}{\stirlingI{p}{1}} (ip)^{k-1} \right) \left( \sum_{k=1}^{e} \stirlingI{v+1}{k} (up)^{k-1} \right). \end{aligned}

We further define

fp,e(u):=∏i=0u−1∑k=1e\stirlingIpk\stirlingIp1(ip)k−1 mod pe=∏i=0u−1(1+∑k=1e−1\stirlingIpk+1\stirlingIp1(ip)k) mod pe.f_{p, e}(u) := \prod_{i=0}^{u-1} \sum_{k=1}^{e} \frac{\stirlingI{p}{k}}{\stirlingI{p}{1}} (ip)^{k-1} \bmod p^e = \prod_{i=0}^{u-1} \left( 1 + \sum_{k=1}^{e-1} \frac{\stirlingI{p}{k+1}}{\stirlingI{p}{1}} (ip)^k \right) \bmod{p^e}.

At this point, we have three terms for (n!)p(n!)_p: \stirlingIp1u\stirlingI{p}{1}^u,fp,e(u)f_{p, e}(u) and ∑k=1e\stirlingIv+1k(up)k−1\sum_{k=1}^{e} \stirlingI{v+1}{k} (up)^{k-1}. The first term and the last term are straightforward to compute, so we’ll focus on the middle term (fp,e(u)f_{p, e}(u)).

Here comes a useful observation: fp,e(u)f_{p, e}(u) is polynomial in uu.

Prove fp,e(u)f_{p, e}(u) is polynomial in uu

We here prove a more general statement:

Lemma: Suppose for any 1≤k<e1 \leq k < e, wk(u)w_k(u) is a polynomial in uu, and

g(x,u)=∏i=0u−1(1+∑k=1e−1wk(u)xk)mod  xe,g(x, u) = \prod_{i=0}^{u-1} \left( 1 + \sum_{k=1}^{e-1} w_k(u) x^k \right) \mod x^e,

then gg is a polynomial in uu with a bounded degree

deg⁡ug(x,u)≤λ(e−1),λ:=max⁡1≤k<edeg⁡wk+1k.\deg_u g(x, u) \leq \lambda (e-1), \quad \lambda := \max_{1 \leq k < e} \frac{\deg w_k + 1}{k}.

Proof: For 0≤k<e0 \leq k < e, let hk(u)h_k(u) be the coefficient of xkx^k in g(x,u)g(x, u). We will prove that hk(u)h_k(u) is polynomial in uu with degree ≤λk\leq \lambda k. With the property of hk(u)h_k(u) ahead, we may simple conclude

g(x,u)=∑k=0e−1hk(u)xk⟹deg⁡ug≤max⁡0≤k<edeg⁡hk≤λ(e−1).g(x, u) = \sum_{k=0}^{e-1} h_k(u) x^k \quad \Longrightarrow \quad \deg_u g \leq \max_{0 \leq k < e} \deg h_k \leq \lambda (e-1).

Here we use induction on kk:

  • When k=0k = 0, hk(u)=1h_k(u) = 1.
  • Suppose the statement holds for 0<k<e0 < k < e, we have hk(u)=hk−1(u−1)+∑t=0k−1ht(u−1)wk−t(u−1)=hk−1(0)+∑t=0k−1∑u′≥0u−1ht(u′)wk−t(u′).h_k(u) = h_{k-1}(u-1) + \sum_{t=0}^{k-1} h_t(u-1) w_{k-t}(u - 1) = h_{k-1}(0) + \sum_{t=0}^{k-1} \sum_{u' \geq 0}^{u-1} h_t(u') w_{k-t}(u'). As ht(u′)wk−t(u′)h_t(u') w_{k-t}(u') is polynomial in u′u' with degree ≤deg⁡ht+deg⁡wk−t\leq \deg h_t + \deg w_{k-t}, hkh_k is also polynomial in uu with degree deg⁡hk≤max⁡0≤t<k(deg⁡ht+deg⁡wk−t+1),\deg h_k \leq \max_{0 \leq t < k} \left( \deg h_t + \deg w_{k-t} + 1 \right), where the additional constant of 1 comes from summation over u′u'. The definition of λ\lambda and induction give deg⁡hk≤max⁡0≤t<k(λt+λ(k−t))=λk.\deg h_k \leq \max_{0 \leq t < k} \left( \lambda t + \lambda (k - t) \right) = \lambda k. Indeed the recursion on deg⁡hk\deg h_k is just a Knapsack problem, so a trivial upper bound is just the item with largest density.

QED.

Bound the degree of fp,e(u)f_{p, e}(u)

We still have one question to answer: given that fp,e(u)f_{p, e}(u) is a polynomial, how do we bound its degree? We’d like to use the lemma above, but the problem, how do we define wk(u)w_k(u)?

If we use the original definition, we may have

fp,e(u)=∏i=0u−1(1+∑k=1e−1\stirlingIpk+1\stirlingIp1(ip)k)mod  pe,f_{p, e}(u) = \prod_{i=0}^{u-1} \left( 1 + \sum_{k=1}^{e-1}\frac{\stirlingI{p}{k+1}}{\stirlingI{p}{1}} (ip)^k \right) \mod p^e,

that is, 1≤k<e1 \leq k < e, wk(i)=\stirlingIpk+1\stirlingIp1ikw_k(i) = \frac{\stirlingI{p}{k+1}}{\stirlingI{p}{1}} i^k. This definition leads to

λ=max⁡1≤k<edeg⁡wk+1k=max⁡1≤k<ek+1k=2,⟹deg⁡ufp,e(u)≤2(e−1).\lambda = \max_{1 \leq k < e} \frac{\deg w_k + 1}{k} = \max_{1 \leq k < e} \frac{k + 1}{k} = 2, \quad \Longrightarrow \quad \deg_u f_{p, e}(u) \leq 2 (e - 1).

However, prabowo hints that: when pp is prime and 1<k<p1 < k < p, pp divides \stirlingIpk\stirlingI{p}{k}.

We first consider the case of e<pe < p, all relevant \stirlingIpk+1\stirlingI{p}{k+1} is a multiple of pp, and we can extract the common factor:

fp,e=∏i=0u−1(1+∑k=1e−1\stirlingIpk+1\stirlingIp1(ip)k)mod  pe=∏i=0u−1(1+∑k=1e−1\stirlingIpk+1/p\stirlingIp1ikpk+1)mod  pe.\begin{aligned} f_{p, e} & = \prod_{i=0}^{u-1} \left( 1 + \sum_{k=1}^{e-1}\frac{\stirlingI{p}{k+1}}{\stirlingI{p}{1}} (ip)^k \right) \mod p^e \\ & = \prod_{i=0}^{u-1} \left( 1 + \sum_{k=1}^{e-1}\frac{\stirlingI{p}{k+1} / p}{\stirlingI{p}{1}} i^k p^{k+1} \right) \mod p^e. \end{aligned}

Hence, we use wk(i)=\stirlingIpk/p\stirlingIp1ik−1w_k(i) = \frac{\stirlingI{p}{k} / p}{\stirlingI{p}{1}} i^{k-1} for k>1k > 1 and w1(i)=0w_1(i) = 0. The λ\lambda here can be made smaller:

λ=max⁡1≤k<edeg⁡wk+1k=1,⟹deg⁡ufp,e(u)≤e−1.\lambda = \max_{1 \leq k < e} \frac{\deg w_k + 1}{k} = 1, \quad \Longrightarrow \quad \deg_u f_{p, e}(u) \leq e - 1.

(It’s not rigorous here as we can’t divide a number by pp under  mod pe\bmod p^e, although we may do it in R\mathbb{R}. The division here is only used for proof and will not be use in code.)

The other case is e≥pe \geq p, and we have one more Stirling number to discuss: \stirlingIpp=1\stirlingI{p}{p} = 1. We may re-organize the equations:

fp,e=∏i=0u−1(1+∑k=1e−1\stirlingIpk+1\stirlingIp1(ip)k)mod  pe=∏i=0u−1(1+∑k=1p−2\stirlingIpk+1/p\stirlingIp1ikpk+1+1\stirlingIp1(ip)p−1)mod  pe.\begin{aligned} f_{p, e} & = \prod_{i=0}^{u-1} \left( 1 + \sum_{k=1}^{e-1}\frac{\stirlingI{p}{k+1}}{\stirlingI{p}{1}} (ip)^k \right) \mod p^e \\ & = \prod_{i=0}^{u-1} \left( 1 + \sum_{k=1}^{p-2}\frac{\stirlingI{p}{k + 1} / p}{\stirlingI{p}{1}} i^k p^{k+1} + \frac{1}{\stirlingI{p}{1}} (ip)^{p-1} \right) \mod p^e. \end{aligned}

Hence we define for 1<k<p1 < k < p,wk(i)=\stirlingIpk/p\stirlingIp1ikw_k(i) = \frac{\stirlingI{p}{k} / p}{\stirlingI{p}{1}} i^k,and wp−1=\stirlingIpp−1/p\stirlingIp1ip−2+1\stirlingIp1ip−1w_{p-1} = \frac{\stirlingI{p}{p-1}/p}{\stirlingI{p}{1}} {i^{p-2}}+ \frac{1}{\stirlingI{p}{1}} i^{p-1}. In this case, λ\lambda is

λ=max⁡1≤k<edeg⁡wk+1k=pp−1,⟹deg⁡ufp,e(u)≤pp−1(e−1).\lambda = \max_{1 \leq k < e} \frac{\deg w_k + 1}{k} = \frac{p}{p - 1}, \quad \Longrightarrow \quad \deg_u f_{p, e}(u) \leq \frac{p}{p - 1} (e - 1).

As p≥2p \geq 2, this bound is always better than 2(e−1)2(e-1).

Overall, we have

deg⁡ufp,e(u)≤d∗:=⌊p(e−1)p−1⌋.\deg_u f_{p, e}(u) \leq d^* := \left\lfloor \frac{p (e - 1)}{p-1} \right \rfloor.

How nicely it captures both cases!

The divisibility of the Stirling numbers

The proof above makes use of a special property of Stirling numbers of the first kind, which we’ll give a short proof here:

For prime pp and 1<k<p1 < k < p, pp divides \stirlingIpk\stirlingI{p}{k}.

Proof: Consider a polynomial f(x)=∏i=1p−1(x+i)∈Fp[x]f(x) = \prod\limits_{i=1}^{p-1} (x+i) \in \mathbb{F}_p[x]. The Stirling numbers of the first kind satisfy

f(x)=∏i=1p−1(x+i)=∑i=1p\stirlingIpixi−1.f(x) = \prod_{i=1}^{p-1} (x + i) = \sum_{i=1}^p \stirlingI{p}{i} x^{i-1}.

Note that f(0)=(p−1)!≡−1(modp)f(0) = (p-1)! \equiv -1 \pmod{p}, as well as f(x)=0f(x) = 0 for any x≠0x \neq 0. On the other hand, the polynomial g(x)=xp−1−1g(x) = x^{p-1} - 1 has identical values as ff in these pp points, and both ff and gg have degree of p−1p - 1, thus f=gf = g. Now we compare the coefficients of ff and gg:

\stirlingIpi≡{1,i=p,0,1<i<p,−1,i=1.(modp)\stirlingI{p}{i} \equiv \begin{cases} 1, & i = p, \\ 0, & 1 < i < p, \\ -1, & i = 1.\end{cases} \pmod{p}

Algorithm for computing (n!)p mod pe(n!)_p \bmod p^e

  • For 0≤v≤p,0≤k≤e0 \leq v \leq p, 0 \leq k \leq e, preprocess \stirlingIvk\stirlingI{v}{k} with overall complexity O(pe)O(pe);
  • Preprocess fp,ef_{p, e}. As a polynomial with degree of d∗d^* requires d∗+1d^* + 1 points to determine, we may simply brute-force fp,e(u)f_{p, e}(u) for 0≤u≤d∗0 \leq u \leq d^* and use Lagrangian interpolation. The complexity is O(e2)O(e^2).
  • For each query, we simply use the following equation: (n!)p≡\stirlingIp1ufp,e(u)(∑k=1e\stirlingIv+1k(up)k−1)(modpe),(n!)_p \equiv \stirlingI{p}{1}^u f_{p, e}(u) \left( \sum_{k=1}^{e} \stirlingI{v+1}{k} (up)^{k-1} \right) \pmod{p^e}, with time complexity of O(e)O(e) per query.

If d∗≥pd^* \geq p, we may be careful in Lagrange interpolation. The following equation can be helpful,

fp,e(u)=∑i=0d∗fp,e(i)∏j≠iu−ji−j=∑i=0d∗fp,e(i)(uj)(u−j−1i−j)(−1)i−j,f_{p, e}(u) = \sum_{i=0}^{d^*} f_{p, e}(i) \prod_{j \neq i} \frac{u - j}{i - j} = \sum_{i=0}^{d^*} f_{p, e}(i) \binom{u}{j}\binom{u-j-1}{i-j} (-1)^{i-j},

which means that it must be an integer. However, ∏j≠iu−ji−j\prod_{j \neq i} \frac{u - j}{i - j} might be a little tricky as i−ji - j might not be invertible under  mod pe\bmod{p^e}… Anyway we have to represent all numbers in the form of r≡npa(modpe)r \equiv \frac{n}{p^a} \pmod {p^e} and do multiplication/division later…

For reference code, prabowo provides a C++ version while I can share my own Python version. Unfortunately, min_25’s Python version was lost.

Hexo

When I wrote the blog post for the first time, I use commands like \newcommand{\aaa}[1]{#1} while Hexo recognize # as SwigTags, which leads to a rendering issue. The solution is quite simple: just add

disableNunjucks: true

to the front-matter. Please refer to this issue (there is a typo in disableNunjucks in the comment though) and this issue.