跳到正文

算法·理论

An easier method of polynomial composition: No more Lagrange inversion is needed!

SleeplessSouris约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟

版本

  1. v3当前
  2. v2
  3. v1

Just some random thoughts arised by a simple problem. But it seems that I’m reinventing wheels once more :(

If there’s any mistake in the article, please don’t hesitate to point it out in the comment.

Introduction

Yesterday night, my friend asked me a question on polynomials (his profile picture is pixelated in order to prevent privacy issues):

(Translation: Ciallo! Is there any solution for [xn]fk(x)[x^n]f^k(x) for all k=1∼nk=1\sim n in O(n2)\mathcal O(n ^ 2) time complexity that is easy to implement?)

And unfortunately I didn’t figure out the solution at first. I even considered it as an unsolvable problem:

(Translation: It’s actually unsolvable. I guess, isn’t it?)

But I suddenly realized that in the latest algorithm for polynomial composition, we have done a similar process working on fk(x)f^k(x).

So it might be solvable in O(nlog⁡2n)\mathcal O(n\log ^ 2n)?

Why not give it a try?

Solution

We need to get coefficients for every possible kk. Let’s introduce another term yy to control the number of ff in a specific product, and rewrite the coefficient as [ykxn]1+yf(x)+y2f2(x)+⋯=[ykxn]11−yf(x)[y^kx^n]1+yf(x)+y^2f^2(x)+\dots=[y^kx^n]\frac{1}{1-yf(x)}.

Do you remember how we handled the linear homogeneous recursions using polynomials? We transformed it into an evaluation of a certain coefficient, in the form of a fraction: [xk]P(x)Q(x)[x^k]\frac{P(x)}{Q(x)}. We apply Bostan-Mori algorithm on this fraction afterwards.

For the bivariate polynomial with x,yx, y, we could apply a similar technique. Let’s say if we are now finding the coefficient [ykxn]P(x,y)Q(x,y)[y^kx^n]\frac{P(x, y)}{Q(x, y)}. We multiply by Q(−x,y)Q(-x, y) on both numerator and denominator to eliminate odd powers of xx on the denominator, resulting in [ykxn]P(x,y)Q(−x,y)Q′(x2,y)[y^kx^n]\frac{P(x, y)Q(-x, y)}{Q'(x ^ 2, y)}, where Q′(x2,y)=Q(x,y)Q(−x,y)Q'(x^2, y) = Q(x, y)Q(-x, y).

After this iteration, we can only conserve about half of the elements depending on the parity of nn: if nn is even, we rewrite it as P′(x2,y)Q′(x2,y)\frac{P'(x^2, y)}{Q'(x^2, y)}, otherwise we rewrite it as xP′(x2,y)Q′(x2,y)\frac{xP'(x^2, y)}{Q'(x^2, y)}. Now the problem is reduced to a subproblem with same form with the degree of xx halved. When n=0n = 0, the variable xx is entirely eliminated, and we only need to get [yk]P(y)Q(y)[y^k]\frac{P(y)}{Q(y)} using polynomial inversion in O(nlog⁡n)\mathcal O(n\log n) time. (Can be done in O(n)\mathcal O(n), but not necessary.)

Consider the time complexity of the algorithm: after each iteration, although the degree of yy is lifted to twice as before, but the degree of xx is halved. Thus, for every polynomial involved in the calculation, the product of degree for x,yx, y is always O(n)\mathcal O(n), so we can multiply two polynomials within O(nlog⁡n)\mathcal O(n\log n) time.

We handled O(log⁡n)\mathcal O(\log n) multiplications, because after this number of iterations we will drop into the corner case of n=0n = 0. Thus, the whole time complexity is O(nlog⁡2n)\mathcal O(n\log ^ 2 n).

Polynomial Composition

Now let us move on to polynomial composition. Suppose we have two polynomials f,gf, g, and a new polynomial g(f(x))=g0+g1f1(x)+g2f2(x)+⋯+gnfn(x)g(f(x)) = g_0+g_1f^1(x)+g_2f^2(x)+\dots+g_nf^n(x) is required. (Assume that calculation is done under  mod  xn+1\bmod \ x^{n + 1}.) The expression seems to have many similarities with the previous question.

We need to assign a coefficient for each single fk(x)f^k(x) depending on g(x)g(x). We can still maintain the variable yy, since we know that the term with yky^k should be multiplied by gkg_k.

Thus, we are actually multiplying the whole expression with g0y0+g1y−1+g2y−2+⋯+gny−ng_0y^0+g_1y^{-1}+g_2y^{-2}+\dots+g_ny^{-n} to let every valid term be of y0y^0. With the help of this polynomial, the answer could be written as:

[y0]g(y−1)1−yf(x) mod  xn+1[y^0]\frac{g(y^{-1})}{1-yf(x)}\bmod \ x^{n + 1}

The following step is just the same as we have done in the previous part. Moreover, we need to store every coefficient from x0x^0 to xnx^n, which is slightly different. But an advantage is that the numerator only relates to yy, instead of both x,yx, y.

Suppose we are handling P(y)Q(x,y)\frac{P(y)}{Q(x, y)}. Still multiplying by Q(−x,y)Q(-x, y), getting P(y)Q(−x,y)Q′(x2,y)\frac{P(y)Q(-x, y)}{Q'(x ^ 2, y)}. But the problem is: if we still do casework on the parity of nn, we will get two branches, leading to wrong time complexity as T(m)=2T(m/2)+O(nlog⁡n)T(m)=2T(m/2)+\mathcal O(n\log n).

Can we avoid operations on xx? Putting P(y)Q′(x2,y)\frac{P(y)}{Q'(x^2, y)} into the recursion might be a good choice. Now we have the results for P(y)Q′(x2,y)\frac{P(y)}{Q'(x^2, y)},and we need to multiply it by Q(−x,y)Q(-x, y).

For the first iteration, as we need coefficient of [y0][y ^ 0], we just need to focus on [y0][y^0] and [y−1][y ^ {-1}] of P(y)Q′(x2,y)\frac{P(y)}{Q'(x^2, y)}, since deg⁡y(Q)=1\deg_y(Q) = 1. Now deg⁡y(Q′)=2\deg_y(Q')=2, and we need [y0],[y−1],[y−2],[y−3][y^0], [y^{-1}],[y^{-2}], [y^{-3}] in the next iteration. So the number of needed values is depending on deg⁡y(Q)\deg_y(Q). More specifically, we just need to figure out [y−deg⁡y(Q)+1]∼[y0][y^{-\deg_y(Q) + 1}]\sim [y^0] in order to correctly deal with the previous iterations.

As we have said in the previous part, deg⁡xdeg⁡y=O(n)\deg_x\deg_y = \mathcal O(n), and ∑deg⁡y(Q)=∑k=0log⁡nO(2k)=O(n)\sum \deg_y(Q)=\sum \limits_{k = 0}^{\log n}\mathcal O(2^k) = \mathcal O(n), so the time complexity is still O(nlog⁡n)\mathcal O(n\log n) per iteration, O(nlog⁡2n)\mathcal O(n\log ^ 2 n) in total. When n=0n = 0, an inverse is needed, which can also be done in O(nlog⁡n)\mathcal O(n \log n).

Thus, polynomial composition under modulo xn+1x^{n + 1} is solved in O(nlog⁡2n)\mathcal O(n\log ^2 n) time complexity.

Code

Not written yet.

References:

  1. Yasunori Kinoshita, Baitian Li: Power Series Composition in Near-Linear Time.

  2. alpha1022: 多项式不存在了:多项式复合(逆)的 O(nlog^2n) 做法.

原文