Skip to main content

Polynomial Algorithms

· 11 min read
Xinyu Ma
Research Scientist @ Meta

This post introduces some tricks on polynomials widely used in ICPC. I will try to practice algebraic knowledge as well.

Polynomial Inverse​

Prop: Let RR be a commutative ring and II be an ideal. Then [p]I[p ]_I is a unit in R/IR/I if and only if ⟨p⟩\langle p\rangle is coprime with II.

Proof: 1∈Rp+I1\in Rp+I is equivalent to ⟨p⟩+I=R\langle p\rangle+I = R.

Cor: For RR UFD, f∈R[X]/⟨Xn⟩f\in R[ X]/\langle X^n\rangle is a unit if and only if f(0)f(0) is a unit in RR.

Proof: RR being a domain implies XaX^a (a<na< n) are the only factors of XnX^n. Suppose f(0)f(0) is a unit, then ff is coprime with XnX^n in K[X]K[ X], where KK is the fractional field of RR. Suppose fg=1fg = 1 in K[X]K[ X]. Since the content of ff is associated to 11, by Gauß's lemma, C(fg^)C(f\hat{g}) is also associated to 11. Since fg^=h⋅Xn+f(0)g^(0)f\hat{g} = h\cdot X^n + f(0)\hat{g}(0), [f(0)g^(0)]−1g^[ f(0)\hat{g}(0)]^{-1}\hat{g} is the inverse of ff modulo XnX^n. The "only if" part is trivial.

Prop: If fg=h⋅Xn+1fg = h\cdot X^n + 1, then (2−fg)g⋅f=−h2⋅X2n+1(2-fg)g\cdot f = -h^2\cdot X^{2n} +1.

This proposition gives a binary lifting algorithm to calculate f−1f^{-1} modulo XnX^n, in O(nlog⁡n)O(n\log n).

Newton's Method​

Let RR be a commutative ring and f:=∑i=0∞biXi∈R[[X]]f:= \sum_{i=0}^{\infty} b_iX^i \in R[[ X]] be a power series. Suppose f(a)∈Rpf(a)\in Rp for some a,p∈Ra,p\in R, i.e. aa is a root to ff modulo pp. Then, use the substitution X=a+YpX = a+Yp we have

f(X)=∑ibi(a+Yp)i=∑ibiai+∑iibi+1aiYp+p2Y2(…)=f(a)+Ypf′(a)+p2Y2(…)\begin{darray}{rcl} f(X) &=& \sum_i b_i(a+Yp)^i \\ &=& \sum_i b_ia^i + \sum_i ib_{i+1}a^iYp + p^2Y^2\left(\ldots\right) \\ &=& f(a) + Ypf'(a) + p^2Y^2\left(\ldots\right) \end{darray}

Therefore, we have f(a+Yp)∈Rp2f(a+Yp)\in Rp^2 iff f(a)+f′(a)pY∈Rp2f(a) + f'(a)pY \in Rp^2. Since p∣f(a)p\mid f(a), if f′(a)f'(a) is invertible in R/p2RR/p^2R, we have p∣f(a)f′(a)−1p\mid f(a)f'(a)^{-1}. Thus, Y=a−f(a)f′(a)−1Y = a - f(a)f'(a)^{-1} is a root to ff modulo p2p^2 in this case. Furthermore, if RR is a UFD then f′(a)f'(a) is invertible in R/p2RR/p^2R iff p∤f′(a)p \nmid f'(a).

Now, substitute RR with R[X]R[ X] and pp with XmX^m. Consider g∈R[X,Y]g\in R[ X, Y], but it can have infinite terms on YY. If there is some a∈Ra\in R s.t. g(0,a)=0,∂g∂Y(0,a)∈R×g(0, a) = 0, \frac{\partial g}{\partial Y}(0, a)\in R^{\times}, then we can calculate the "root" of g(X,f(X))g(X, f(X)) in R[X]/⟨Xn⟩R[ X]/\langle X^n\rangle for any nn:

  • Let f0(X)=af_0(X) = a and n0=1n_0 = 1
  • Let fi(X)=a−g(X,fi−1)(∂g∂Y(0,fi−1))−1f_i(X) = a - g(X, f_{i-1}) \left(\frac{\partial g}{\partial Y}(0, f_{i-1})\right)^{-1} and ni=2ni−1n_i = 2n_{i-1}.
    • Note that X∣g(X,fi−1)X\mid g(X, f_{i-1}). Thus, if the first derivative is invertible, all derivatives are invertible.

Examples​

  • Inverse h(X)h(X): let g(X,Y)=1Y−h(X)g(X, Y) = \frac{1}{Y} - h(X). Get fi+1=2fi−fi2hf_{i+1} = 2f_{i} - f_i^2h.
    • Condition: h(0)h(0) is invertible.
  • Sqrt of h(X)h(X): let g(X,Y)=Y2−h(X)g(X, Y) = Y^2 - h(X). Get fi+1=12(fi2+h)fi−1f_{i+1} = \frac{1}{2}(f_i^2+h)f_i^{-1}.
    • Condition: there exists aa s.t. a2=h(0)a^2 = h(0) and 2a∈R×2a\in R^{\times}.
    • For exmaple, let R=Z/9ZR = \mathbb{Z}/9\mathbb{Z} and h(X)=X+1h(X) = X+1. Starting from f0=1f_0 = 1 and 2×5=12\times 5 = 1, we can get f2(X)=−5X3+X2+5X+1f_2(X)= -5X^3+X^2+5X+1 is the solution modulo X4X^4.
      • Well, RR is not a domain, but it works.
  • Exp of h(X)h(X): let g(X,Y)=ln⁡Y−h(X)g(X, Y) = \ln Y - h(X). Get fi+1=fi(X)(1−ln⁡fi(X)+h(X))f_{i+1} = f_i(X)(1 - \ln {f_i(X)} + h(X)).
    • Log can be calculated by integral on f′(X)f(X)\frac{f'(X)}{f(X)}.
    • Condition: h(0)=0h(0)=0

Polynomial Modulus​

Let KK be a field. For f∈K[X]f\in K[ X] of degree dd, let fT(X):=Xdf(1/X)f^T(X) := X^df(1/X) be the reverse of coefficients of ff. Suppose f=gq+rf = gq + r, where f,gf, g are of degree n,mn, m resp. Then, fT(X)=gT(X)qT(X)+Xnr(1/X)f^T(X) = g^T(X)q^T(X) + X^nr(1/X). Since rr is of degree at most m−1m-1, Xn−m+1∣Xnr(1/X)X^{n-m+1}\mid X^nr(1/X). Also, q(X)q(X) is of degree n−m<n−m+1n-m < n-m+1. Thus, q=[fT⋅(gT)−1]T(mod Xn−m+1)q = [ f^T\cdot (g^T)^{-1}]^T (\text{mod }X^{n-m+1}) and r=f−gqr = f - gq.

FFT​

Let AA be an RR-algebra, and g∈A×g\in A^{\times} of order nn. Suppose ∑i=0n−1gik=0\sum_{i=0}^{n-1}g^{ik} = 0 for all 1≤k<n1\leq k < n (generally true if gk−1g^k-1 is not a zero divisor). For a sequence a=(a0,…,an−1)∈Rna = (a_0, \ldots, a_{n-1})\in R^n, its discrete Fourier transform is F(a)=(F0(a),…,Fn−1(a))∈AnF(a) = (F_0(a), \ldots, F_{n-1}(a))\in A^n where

Fk(a)=∑i=0n−1aigikF_k(a) = \sum_{i=0}^{n-1}a_ig^{ik}

If nn is invertible in AA, the inverse of DFT is

ai=1n∑k=0n−1Fk(a)g−ika_i = \frac{1}{n}\sum_{k=0}^{n-1} F_k(a)g^{-ik}

This is given by the zero sum condition.

The DFT of the cyclic convolution a∗ba * b is the pointwise product of their DFT:

Fk(a∗b)=Fk(a)Fk(b)(a∗b)i=∑j=0n−1ajb(i−j)mod  n\begin{darray}{rcl} F_k(a*b) &=& F_k(a)F_k(b) \\ (a*b)_i &=& \sum_{j=0}^{n-1} a_j b_{(i-j)\mod n} \end{darray}

Thus, DFT can be used to compute convolution. However, directly computing the sum does not reduce the time complexity. Now we try to accelerate this. Suppose n=2mn = 2^m.

Fk(a)=∑i=02m−1aigik=(∑i=02m−1−1a2igi⋅2k)+gk(∑i=02m−1−1a2i+1gi⋅2k)=Fk′(aeven)+Fk′(aodd)\begin{darray}{rcl} F_k(a) &=& \sum_{i=0}^{2^m-1}a_ig^{ik} \\ &=& \left(\sum_{i=0}^{2^{m-1}-1}a_{2i}g^{i\cdot 2k}\right) + g^k\left(\sum_{i=0}^{2^{m-1}-1}a_{2i+1}g^{i\cdot 2k}\right) \\ &=& F_k'(a_{even}) + F_k'(a_{odd}) \end{darray}

where Fk′F_k' is the DFT of length n/2n/2 using g2g^2 as the primitive root instead of gg. Repeating this, we get O(nlog⁡n)O(n\log n) fast Fourier transform (FFT) algorithm.

Common settings​

  • R=ZR=\mathbb{Z}, A=CA=\mathbb{C}, g=e2πing = e^{\frac{2\pi i}{n}}
  • R=A=Z/pR=A=\mathbb{Z}/p, for some prime number p=c⋅2m+1p = c\cdot 2^m + 1.
    • p=0xC0000001p = 0\text{x}\text{C}0000001, g=5g = 5, m=30m = 30
    • p=0x78000001p = 0\text{x}78000001, g=22g = 22, m=27m = 27
    • p=0x3B800001p = 0\text{x}3\text{B}800001, g=3g = 3, m=23m = 23
    • p=0x0A000001p = 0\text{x}0\text{A}000001, g=3g = 3, m=25m = 25

Lagrange polynomial​

Let KK be a field and f∈K[X]f\in K[ X] is of degree dd. If we know its value on d+1d+1 different points x0,…,xd∈Kx_0, \ldots, x_d\in K and f(x0),…,f(xd)∈Kf(x_0), \ldots, f(x_d)\in K, then we can recover the coefficients of ff.

Let li∈K[X]l_i\in K[ X] be defined as:

li(X):=∏j≠iX−xjxi−xjl_i(X) := \prod_{j\neq i}\frac{X - x_j}{x_i - x_j}

Then, lil_i is 11 at xix_i and 00 at other points xj≠xix_j\neq x_i. Let l:=∑i=0dyilil := \sum_{i=0}^{d} y_il_i, then f=lf = l. Because otherwise, f−lf-l will be a polynomial of degree dd but have d+1d+1 disjoint roots.

To evaluate f(x)f(x), we can directly substitute xx into lil_i, and get an O(d2)O(d^2) algorithm.

Example: sum of powers​

Let fk(n)=∑i=1nikf_k(n) = \sum_{i=1}^{n} i^k. By induction we can prove fkf_k is a polynomial of degree k+1k+1. To calculate fk(n)f_k(n), it suffices to compute fk(0),…,fk(k+1)f_k(0), \ldots, f_k(k+1) and then interpolate.

Multipoint Evaluation​

Let RR be an commutative ring, f∈R[X]f\in R[ X] be a polynomial with degree less than nn, x1,…,xn∈Rx_1,\ldots, x_n\in R be points. Compute f(x1),…,f(xn)f(x_1), \ldots, f(x_n).

Let p0(X)=∏i=1⌊n/2⌋(X−xi)p_0(X) = \prod_{i=1}^{\lfloor n/2 \rfloor} (X - x_i) and p1(X)=∏i=⌊n/2⌋+1n(X−xi)p_1(X) = \prod_{i=\lfloor n/2 \rfloor+1}^{n} (X - x_i). Define r0(X)=fmod  p0r_0(X) = f\mod p_0 and r1(X)=fmod  p1r_1(X) = f\mod p_1. Then it suffices to evaluate r0r_0 on x1,…,x⌊n/2⌋x_1,\ldots, x_{\lfloor n/2 \rfloor} and r1r_1 on x⌊n/2⌋+1,…,xnx_{\lfloor n/2 \rfloor+1},\ldots, x_{n}, as f(x)=r0(x)f(x) = r_0(x) for all x∈{x1,…,x⌊n/2⌋}x\in \{x_1, \ldots, x_{\lfloor n/2 \rfloor} \}. This divide-and-conquer algorithm works in O(nlog⁡2n)O(n\log^2 n).

In special case there are algorithms that run faster, e.g. O(nlog⁡n)O(n\log n), but need more memory.

Example: factorial​

Let f(X):=∏i=1n(X+i)f(X) := \prod_{i=1}^{n}(X+i). Then, n!=f(0)n! = f(0). Let g(X):=∏i=1v(X+i)g(X) := \prod_{i=1}^{v}(X+i), where v=⌊n⌋v = \lfloor\sqrt{n}\rfloor. Then,

n!=(∏i=0v−1g(vi))⋅∏i=v2+1nin! = \left( \prod_{i=0}^{v-1}g(vi) \right)\cdot \prod_{i=v^2+1}^{n}i

Thus, it is enough to calculate g(X)g(X) on 0,v,…,v(v−1)0, v, \ldots, v(v-1).

Recursive Sequence​

Suppose {ai}∞\{a_i\}_ {\infty} is a sequence s.t. ai=∑j=1dcjai−ja_i = \sum_{j=1}^{d} c_ja_{i-j}. Then, the array is decided by its first nn elements.

Matrix Exponentiation​

Clearly, we have

(an+d−1an+d−2an+d−3⋮an)=(c1c2⋯cd−1cd10⋯0001⋯00⋮⋮⋱⋮⋮00⋯10)n⋅(ad−1ad−2ad−3⋮a0)\begin{pmatrix} a_{n+d-1} \\ a_{n+d-2} \\ a_{n+d-3} \\ \vdots \\ a_n \end{pmatrix} = \begin{pmatrix} c_1 & c_2 & \cdots & c_{d-1} & c_d \\ 1 & 0 & \cdots & 0 & 0 \\ 0 & 1 & \cdots & 0 & 0 \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ 0 & 0 & \cdots & 1 & 0 \end{pmatrix}^n \cdot \begin{pmatrix} a_{d-1} \\ a_{d-2} \\ a_{d-3} \\ \vdots \\ a_0 \end{pmatrix}

Thus, we can let MM denote the recurrenc matrix and calculate MnM^n in O(d3log⁡n)O(d^3\log n):

  • M2m=(Mm)2M^{2m} = (M^m)^2
  • M2m+1=(Mm)2⋅MM^{2m+1} = (M^m)^2\cdot M

Polynomial Modulus​

By Hamilton-Cayley theorem, the characteristic polynomial of MM is an annihilator of MM. Let p(X):=Xd−∑i=1dciXd−ip(X) := X^d - \sum_{i=1}^{d}c_iX^{d-i} and rn(X):=Xnmod  pr_n(X) := X^n\mod p. Then, An=rn(A)A^n = r_n(A). Thus, if rn(X)=∑i=0d−1siXir_n(X) = \sum_{i=0}^{d-1} s_iX^i, then an=∑i=0d−1siaia_n = \sum_{i=0}^{d-1} s_ia_i.

However, there is a much cleaner way to view this. Recall that given c∈Rc\in R, we have R[X]/⟨X−c⟩≅RR[ X]/\langle X-c \rangle\cong R via X↦cX\mapsto c. This somehow gives an solution for ai=cai−1a_i = ca_{i-1} as ai=cia_i = c^i. We want to generalize this, but a1×a1≠a2a_1 \times a_1 \neq a_2. This inspires us that we should forget the multiplication.

Consider R[X]R[ X] as a abelian group. Then, define f:R[X]→Rf: R[ X]\to R as f(uXi)=u⋅aif(uX^i) = u\cdot a_i for all u∈Ru\in R and i∈Ni\in\mathbb{N}. By the distributivity of RR, ff is a well-defined group homomorphism. Let I=⟨p⟩I = \langle p \rangle be the ideal generated by pp. We claim that II is in the kernel of ff. Since ff preserves the addition, so it suffices to show that f(uXip)=0f(uX^ip) = 0 for all u∈Ru\in R and i∈Ni\in\mathbb{N}. This is clearly true since

u(ai+d−c1ai+d−1−…−cdai)=u⋅0=0u(a_{i+d} - c_1a_{i+d-1} - \ldots - c_da_{i}) = u\cdot 0 = 0

Observe that now RR becomes a cocone of the diagram 1←I↪R1\leftarrow I \hookrightarrow R. And R/IR/I is exactly the coproduct of this diagram, with canonical map π:R→R/I\pi: R\to R/I via f↦(fmod  p)f\mapsto (f\mod p). By the universality, f=f↾R/I∘πf = f\restriction_{R/I}\circ \pi. Hence, an=f(Xnmod  p)=∑i=0d−1siaia_n = f(X^n\mod p) = \sum_{i=0}^{d-1} s_ia_i, with sis_i defined above.

Berlekamp-Massey Algorithm​

This is the inverse problem: given a recurrence sequence aiinfty\\{a_i\\}_ {\\infty} (first 2d2d elements are enough), can we compute its recurrence relation ai=∑j=1dcjai−ja_i = \sum_{j=1}^{d} c_ja_{i-j}?

Again we use a polynomial to represent the recurrence relation. Suppose pi∈R[X]p_i\in R[ X] s.t. pip_i works for aja_j, 0≠j<i0\neq j < i. Formally, this means f(Xjmod  pi)=ajf(X^j\mod p_i) = a_j and f(Xj−deg⁡pipi)=0f(X^{j-\deg p_i}p_i) = 0. Let the error

ei=ai+∑j=1deg⁡pi[Xdeg⁡pi−j]pi⋅ai−j=f(Xi−deg⁡pipi)e_i = a_i + \sum_{j=1}^{\deg p_i} [ X^{\deg p_i-j}]p_i\cdot a_{i-j} = f(X^{i-\deg p_i}p_i)
  • If ei=0e_i = 0, we can keep it pi+1=pip_{i+1} = p_i.
  • If ei≠0e_i \neq 0, we need to fix it.
    • Suppose pk≠pip_k\neq p_i is some polynomial we have different from pip_i. Let pi+1=pi+eiekpkXi−kp_{i+1} = p_i + \frac{e_i}{e_k}p_kX^{i-k}.

Clearly, f(Xi−deg⁡pi+1pi+1)=0f(X^{i-\deg p_{i+1}} p_{i+1})=0, so pi+1p_{i+1} works for aia_{i}. One can verify it works for all aja_j, 0≤j≤i0\leq j\leq i.

Now we want to minimize the degree of final pp. The flexibility we have is pkp_k when fixing the error. Massey argues that we can use the last pkp_k s.t. deg⁡pk+1>deg⁡pk\deg p_{k+1} > \deg p_k.

References​