版本比较
An easier method of polynomial composition: No more Lagrange inversion is needed!
正文
增加 11 行删除 7 行
1234567891011121314151617181920215 行未改动27282930313233343536373839404142433 行未改动47484950515253545556575859606162636465### IntroductionYesterday 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 $[x^n]f^k(x)$ for all $k=1\sim n$ in $\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 $f^k(x)$.So it might be solvable in $\mathcal O(n\log ^ 2n)$?Why not give it a try?### Solution5 行未改动For the bivariate polynomial with $x, y$, we could apply a similar technique. Let's say if we are now finding the coefficient $[y^kx^n]\frac{P(x, y)}{Q(x, y)}$. We multiply by $Q(-x, y)$ on both numerator and denominator to eliminate odd powers of $x$ on the denominator, resulting in $[y^kx^n]\frac{P(x, y)Q(-x, y)}{Q'(x ^ 2, y)}$, where $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 $n$: if $n$ is even, we rewrite it as $\frac{P'(x^2, y)}{Q'(x^2, y)}$, otherwise we rewrite it as $\frac{xP'(x^2, y)}{Q'(x^2, y)}$. Now the problem is reduced to a subproblem with same form with the degree of $x$ halved. When $n = 0$, the variable $x$ is entirely eliminated, and we only need to get $[y^k]\frac{P(y)}{Q(y)}$ using polynomial inversion in $\mathcal O(n\log n)$ time. (Can be done in $\mathcal O(n)$, but not necessary.)Consider the time complexity of the algorithm: after each iteration, although the degree of $y$ is lifted to twice as before, but the degree of $x$ is halved. Thus, for every polynomial involved in the calculation, the product of degree for $x, y$ is always $\mathcal O(n)$, so we can multiply two polynomials within $\mathcal O(n\log n)$ time.We handled $\mathcal O(\log n)$ multiplications, because after this number of iterations we will drop into the corner case of $n = 0$. Thus, the whole time complexity is $\mathcal O(n\log ^ 2 n)$.### Polynomial CompositionNow let us move on to polynomial composition. Suppose we have two polynomials $f, g$, and a new polynomial $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 $\bmod \ x^{n + 1}$.) The expression seems to have many similarities with the previous question.We need to assign a coefficient for each single $f^k(x)$ depending on $g(x)$. We can still maintain the variable $y$, since we know that the term with $y^k$ should be multiplied by $g_k$.Thus, we are actually multiplying the whole expression with $g_0y^0+g_1y^{-1}+g_2y^{-2}+\dots+g_ny^{-n}$ to let every valid term be of $y^0$. With the help of this polynomial, the answer could be written as:$$[y^0]\frac{g(y^{-1})}{1-yf(x)}\bmod \ x^{n + 1}$$3 行未改动Suppose we are handling $\frac{P(y)}{Q(x, y)}$. Still multiplying by $Q(-x, y)$, getting $\frac{P(y)Q(-x, y)}{Q'(x ^ 2, y)}$. But the problem is: if we still do casework on the parity of $n$, we will get two branches, leading to wrong time complexity as $T(m)=2T(m/2)+\mathcal O(n\log n)$.Can we avoid operations on $x$? Putting $\frac{P(y)}{Q'(x^2, y)}$ into the recursion might be a good choice. Now we have the results for $\frac{P(y)}{Q'(x^2, y)}$,and we need to multiply it by $Q(-x, y)$.For the first iteration, as we need coefficient of $[y ^ 0]$, we just need to focus on $[y^0]$ and $[y ^ {-1}]$ of $\frac{P(y)}{Q'(x^2, y)}$, since $\deg_y(Q) = 1$. Now $\deg_y(Q')=2$, and we need $[y^0], [y^{-1}],[y^{-2}], [y^{-3]}]$ in the next iteration. So the number of needed values is depended on $\deg_y(Q)$. More specifically, we just need to figure out $[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_x\deg_y = \mathcal O(n)$, and $\sum \deg_y(Q)=\sum \limits_{k = 0}^{\log n}\mathcal O(2^k) = \mathcal O(n)$, so the time complexity is still $\mathcal O(n\log n)$ per iteration, $\mathcal O(n\log ^ 2 n)$ in total. When $n = 0$, an inverse is needed, which can also be done in $\mathcal O(n \log n)$.Thus, polynomial composition under modulo $x^{n + 1}$ is solved in $\mathcal O(n\log ^2 n)$ time complexity.### CodeNot written yet.### References:1. Yasunori Kinoshita, Baitian Li: [Power Series Composition in Near-Linear Time](https://arxiv.org/pdf/2404.05177).2. alpha1022: [多项式不存在了:多项式复合(逆)的 O(nlog^2n) 做法](https://www.luogu.com.cn/article/7joh5isi).123456789101112131415161718192021222324255 行未改动31323334353637383940414243444546473 行未改动51525354555657585960616263646566676869Just 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.### IntroductionYesterday 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 $[x^n]f^k(x)$ for all $k=1\sim n$ in $\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 $f^k(x)$.So it might be solvable in $\mathcal O(n\log ^ 2n)$?Why not give it a try?### Solution5 行未改动For the bivariate polynomial with $x, y$, we could apply a similar technique. Let's say if we are now finding the coefficient $[y^kx^n]\frac{P(x, y)}{Q(x, y)}$. We multiply by $Q(-x, y)$ on both numerator and denominator to eliminate odd powers of $x$ on the denominator, resulting in $[y^kx^n]\frac{P(x, y)Q(-x, y)}{Q'(x ^ 2, y)}$, where $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 $n$: if $n$ is even, we rewrite it as $\frac{P'(x^2, y)}{Q'(x^2, y)}$, otherwise we rewrite it as $\frac{xP'(x^2, y)}{Q'(x^2, y)}$. Now the problem is reduced to a subproblem with same form with the degree of $x$ halved. When $n = 0$, the variable $x$ is entirely eliminated, and we only need to get $[y^k]\frac{P(y)}{Q(y)}$ using polynomial inversion in $\mathcal O(n\log n)$ time. (Can be done in $\mathcal O(n)$, but not necessary.)Consider the time complexity of the algorithm: after each iteration, although the degree of $y$ is lifted to twice as before, but the degree of $x$ is halved. Thus, for every polynomial involved in the calculation, the product of degree for $x, y$ is always $\mathcal O(n)$, so we can multiply two polynomials within $\mathcal O(n\log n)$ time.We handled $\mathcal O(\log n)$ multiplications, because after this number of iterations we will drop into the corner case of $n = 0$. Thus, the whole time complexity is $\mathcal O(n\log ^ 2 n)$.### Polynomial CompositionNow let us move on to polynomial composition. Suppose we have two polynomials $f, g$, and a new polynomial $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 $\bmod \ x^{n + 1}$.) The expression seems to have many similarities with the previous question.We need to assign a coefficient for each single $f^k(x)$ depending on $g(x)$. We can still maintain the variable $y$, since we know that the term with $y^k$ should be multiplied by $g_k$.Thus, we are actually multiplying the whole expression with $g_0y^0+g_1y^{-1}+g_2y^{-2}+\dots+g_ny^{-n}$ to let every valid term be of $y^0$. With the help of this polynomial, the answer could be written as:$$[y^0]\frac{g(y^{-1})}{1-yf(x)}\bmod \ x^{n + 1}$$3 行未改动Suppose we are handling $\frac{P(y)}{Q(x, y)}$. Still multiplying by $Q(-x, y)$, getting $\frac{P(y)Q(-x, y)}{Q'(x ^ 2, y)}$. But the problem is: if we still do casework on the parity of $n$, we will get two branches, leading to wrong time complexity as $T(m)=2T(m/2)+\mathcal O(n\log n)$.Can we avoid operations on $x$? Putting $\frac{P(y)}{Q'(x^2, y)}$ into the recursion might be a good choice. Now we have the results for $\frac{P(y)}{Q'(x^2, y)}$,and we need to multiply it by $Q(-x, y)$.For the first iteration, as we need coefficient of $[y ^ 0]$, we just need to focus on $[y^0]$ and $[y ^ {-1}]$ of $\frac{P(y)}{Q'(x^2, y)}$, since $\deg_y(Q) = 1$. Now $\deg_y(Q')=2$, and we need $[y^0], [y^{-1}],[y^{-2}], [y^{-3}]$ in the next iteration. So the number of needed values is depending on $\deg_y(Q)$. More specifically, we just need to figure out $[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_x\deg_y = \mathcal O(n)$, and $\sum \deg_y(Q)=\sum \limits_{k = 0}^{\log n}\mathcal O(2^k) = \mathcal O(n)$, so the time complexity is still $\mathcal O(n\log n)$ per iteration, $\mathcal O(n\log ^ 2 n)$ in total. When $n = 0$, an inverse is needed, which can also be done in $\mathcal O(n \log n)$.Thus, polynomial composition under modulo $x^{n + 1}$ is solved in $\mathcal O(n\log ^2 n)$ time complexity.### CodeNot written yet.### References:1. Yasunori Kinoshita, Baitian Li: [Power Series Composition in Near-Linear Time](https://arxiv.org/pdf/2404.05177).2. alpha1022: [多项式不存在了:多项式复合(逆)的 O(nlog^2n) 做法](https://www.luogu.com.cn/article/7joh5isi).