Skip to main content

Polynomial Algorithms

· 9 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: 1Rp+I1\in Rp+I is equivalent to p+I=R\langle p\rangle+I = R.

Cor: For RR UFD, fR[X]/Xnf\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^=hXn+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=hXn+1fg = h\cdot X^n + 1, then (2fg)gf=h2X2n+1(2-fg)g\cdot f = -h^2\cdot X^{2n} +1.

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

Newton's Method

Let RR be a commutative ring and f:=i=0biXiR[[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,pRa,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)pYRp2f(a) + f'(a)pY \in Rp^2. Since pf(a)p\mid f(a), if f(a)f'(a) is invertible in R/p2RR/p^2R, we have pf(a)f(a)1p\mid f(a)f'(a)^{-1}. Thus, Y=af(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 pf(a)p \nmid f'(a).

Now, substitute RR with R[X]R[ X] and pp with XmX^m. Consider gR[X,Y]g\in R[ X, Y], but it can have infinite terms on YY. If there is some aRa\in R s.t. g(0,a)=0,gY(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]/XnR[ X]/\langle X^n\rangle for any nn:

  • Let f0(X)=af_0(X) = a and n0=1n_0 = 1
  • Let fi(X)=ag(X,fi1)(gY(0,fi1))1f_i(X) = a - g(X, f_{i-1}) \left(\frac{\partial g}{\partial Y}(0, f_{i-1})\right)^{-1} and ni=2ni1n_i = 2n_{i-1}.
    • Note that Xg(X,fi1)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)=1Yh(X)g(X, Y) = \frac{1}{Y} - h(X). Get fi+1=2fifi2hf_{i+1} = 2f_{i} - f_i^2h.
    • Condition: h(0)h(0) is invertible.
  • Sqrt of h(X)h(X): let g(X,Y)=Y2h(X)g(X, Y) = Y^2 - h(X). Get fi+1=12(fi2+h)fi1f_{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 2aR×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)=lnYh(X)g(X, Y) = \ln Y - h(X). Get fi+1=fi(X)(1lnfi(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 fK[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 m1m-1, Xnm+1Xnr(1/X)X^{n-m+1}\mid X^nr(1/X). Also, q(X)q(X) is of degree nm<nm+1n-m < n-m+1. Thus, q=[fT(gT)1]T(mod Xnm+1)q = [ f^T\cdot (g^T)^{-1}]^T (\text{mod }X^{n-m+1}) and r=fgqr = f - gq.

FFT

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

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

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

ai=1nk=0n1Fk(a)gika_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 aba * b is the pointwise product of their DFT:

Fk(ab)=Fk(a)Fk(b)(ab)i=j=0n1ajb(ij)modn\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=02m1aigik=(i=02m11a2igi2k)+gk(i=02m11a2i+1gi2k)=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 FkF_k' is the DFT of length n/2n/2 using g2g^2 as the primitive root instead of gg. Repeating this, we get O(nlogn)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=c2m+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 fK[X]f\in K[ X] is of degree dd. If we know its value on d+1d+1 different points x0,,xdKx_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 liK[X]l_i\in K[ X] be defined as:

li(X):=jiXxjxixjl_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 xjxix_j\neq x_i. Let l:=i=0dyilil := \sum_{i=0}^{d} y_il_i, then f=lf = l. Because otherwise, flf-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, fR[X]f\in R[ X] be a polynomial with degree less than nn, x1,,xnRx_1,\ldots, x_n\in R be points. Compute f(x1),,f(xn)f(x_1), \ldots, f(x_n).

Let p0(X)=i=1n/2(Xxi)p_0(X) = \prod_{i=1}^{\lfloor n/2 \rfloor} (X - x_i) and p1(X)=i=n/2+1n(Xxi)p_1(X) = \prod_{i=\lfloor n/2 \rfloor+1}^{n} (X - x_i). Define r0(X)=fmodp0r_0(X) = f\mod p_0 and r1(X)=fmodp1r_1(X) = f\mod p_1. Then it suffices to evaluate r0r_0 on x1,,xn/2x_1,\ldots, x_{\lfloor n/2 \rfloor} and r1r_1 on xn/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,,xn/2}x\in \{x_1, \ldots, x_{\lfloor n/2 \rfloor} \}. This divide-and-conquer algorithm works in O(nlog2n)O(n\log^2 n).

In special case there are algorithms that run faster, e.g. O(nlogn)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=nv = \lfloor\sqrt{n}\rfloor. Then,

n!=(i=0v1g(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(v1)0, v, \ldots, v(v-1).

Recursive Sequence

Suppose {ai}\{a_i\}_ {\infty} is a sequence s.t. ai=j=1dcjaija_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+d1an+d2an+d3an)=(c1c2cd1cd100001000010)n(ad1ad2ad3a0)\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(d3logn)O(d^3\log n):

  • M2m=(Mm)2M^{2m} = (M^m)^2
  • M2m+1=(Mm)2MM^{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):=Xdi=1dciXdip(X) := X^d - \sum_{i=1}^{d}c_iX^{d-i} and rn(X):=Xnmodpr_n(X) := X^n\mod p. Then, An=rn(A)A^n = r_n(A). Thus, if rn(X)=i=0d1siXir_n(X) = \sum_{i=0}^{d-1} s_iX^i, then an=i=0d1siaia_n = \sum_{i=0}^{d-1} s_ia_i.

However, there is a much cleaner way to view this. Recall that given cRc\in R, we have R[X]/XcRR[ X]/\langle X-c \rangle\cong R via XcX\mapsto c. This somehow gives an solution for ai=cai1a_i = ca_{i-1} as ai=cia_i = c^i. We want to generalize this, but a1×a1a2a_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)=uaif(uX^i) = u\cdot a_i for all uRu\in R and iNi\in\mathbb{N}. By the distributivity of RR, ff is a well-defined group homomorphism. Let I=pI = \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 uRu\in R and iNi\in\mathbb{N}. This is clearly true since

u(ai+dc1ai+d1cdai)=u0=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 1IR1\leftarrow I \hookrightarrow R. And R/IR/I is exactly the coproduct of this diagram, with canonical map π:RR/I\pi: R\to R/I via f(fmodp)f\mapsto (f\mod p). By the universality, f=fR/Iπf = f\restriction_{R/I}\circ \pi. Hence, an=f(Xnmodp)=i=0d1siaia_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=1dcjaija_i = \sum_{j=1}^{d} c_ja_{i-j}?

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

ei=ai+j=1degpi[Xdegpij]piaij=f(Xidegpipi)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 ei0e_i \neq 0, we need to fix it.
    • Suppose pkpip_k\neq p_i is some polynomial we have different from pip_i. Let pi+1=pi+eiekpkXikp_{i+1} = p_i + \frac{e_i}{e_k}p_kX^{i-k}.

Clearly, f(Xidegpi+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, 0ji0\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. degpk+1>degpk\deg p_{k+1} > \deg p_k.

References