Polynomial Shift | Continuous Point-Value Shift

Polynomial Shift

Polynomial shift is the simplest polynomial composition transform: given the coefficients of f(x)=∑i=0nfixif(x)=\sum _ {i=0}^nf_ix^i and a constant cc, compute the coefficients of f(x+c)f(x+c), namely f(x)↦f(x+c)f(x)\mapsto f(x+c).

Divide-and-Conquer Method

Let

f(x)=f0(x)+x⌊n/2⌋f1(x)f(x)=f_0(x)+x^{\left\lfloor n/2\right\rfloor}f_1(x)

Then

f(x+c)=f0(x+c)+(x+c)⌊n/2⌋f1(x+c)f(x+c)=f_0(x+c)+(x+c)^{\left\lfloor n/2\right\rfloor}f_1(x+c)

The coefficient of (x+c)⌊n/2⌋(x+c)^{\left\lfloor n/2\right\rfloor} is a binomial coefficient, so

T(n)=2T(n/2)+O(nlog⁡n)=O(nlog⁡2n)T(n)=2T(n/2)+O(n\log n)=O(n\log^2 n)

where O(nlog⁡n)O(n\log n) is the time complexity of polynomial multiplication.

Taylor Formula Method

Applying the Taylor formula to f(x)f(x) at cc gives

f(x)=f(c)+f′(c)1!(x−c)+f′′(c)2!(x−c)2+⋯+f(n)(c)n!(x−c)nf(x)=f(c)+\frac{f'(c)}{1!}(x-c)+\frac{f''(c)}{2!}(x-c)^2+\cdots +\frac{f^{(n)}(c)}{n!}(x-c)^n

Therefore

f(x+c)=f(c)+f′(c)1!x+f′′(c)2!x2+⋯+f(n)(c)n!xnf(x+c)=f(c)+\frac{f'(c)}{1!}x+\frac{f''(c)}{2!}x^2+\cdots +\frac{f^{(n)}(c)}{n!}x^n

For t≥0t\geq 0, we have

t![xt]f(x+c)=f(t)(c)=∑i=tnfii!ci−t(i−t)!=∑i=0n−tfi+t(i+t)!cii!\begin{aligned} t!\lbrack x^t\rbrack f(x+c)&=f^{(t)}(c)\\ &=\sum _ {i=t}^nf_ii!\frac{c^{i-t}}{(i-t)!}\\ &=\sum _ {i=0}^{n-t}f _ {i+t}(i+t)!\frac{c^i}{i!} \end{aligned}

Let

A0(x)=∑i=0nfn−i(n−i)!xiB0(x)=∑i=0ncii!xi\begin{aligned} A_0(x)&=\sum _ {i=0}^nf _ {n-i}(n-i)!x^i\\ B_0(x)&=\sum _ {i=0}^n\frac{c^i}{i!}x^i \end{aligned}

Then

[xn−t](A0(x)B0(x))=∑i=0n−t([xn−t−i]A0(x))([xi]B0(x))=∑i=0n−tfi+t(i+t)!cii!=t![xt]f(x+c)\begin{aligned} \lbrack x^{n-t}\rbrack (A_0(x)B_0(x))&=\sum _ {i=0}^{n-t} (\lbrack x^{n-t-i}\rbrack A_0(x))(\lbrack x^i\rbrack B_0(x))\\ &=\sum _ {i=0}^{n-t}f _ {i+t}(i+t)!\frac{c^i}{i!}\\ &=t!\lbrack x^t\rbrack f(x+c) \end{aligned}

Binomial Theorem Method

Using the binomial theorem (a+b)n=∑i=0n(ni)aibn−i\displaystyle (a+b)^n=\sum _ {i=0}^n\binom{n}{i}a^ib^{n-i}, we obtain

f(x+c)=∑i=0nfi(x+c)i=∑i=0nfi(∑j=0i(ij)xjci−j)=∑i=0nfii!(∑j=0ixjj!ci−j(i−j)!)=∑i=0nxii!(∑j=infjj!cj−i(j−i)!)\begin{aligned} f(x+c)&=\sum _ {i=0}^nf_i(x+c)^i\\ &=\sum _ {i=0}^nf_i\left(\sum _ {j=0}^i\binom{i}{j}x^jc^{i-j}\right)\\ &=\sum _ {i=0}^nf_ii!\left(\sum _ {j=0}^i\frac{x^j}{j!}\frac{c^{i-j}}{(i-j)!}\right)\\ &=\sum _ {i=0}^n\frac{x^i}{i!}\left(\sum _ {j=i}^{n}f_jj!\frac{c^{j-i}}{(j-i)!}\right) \end{aligned}

This gives the same result as the method above.

Continuous Point-Value Shift

Example: LOJ 166 — Lagrange interpolation 2

Given a polynomial ff of degree at most nn, together with its consecutive point values f(0),f(1),…,f(n)f(0),f(1),\dots ,f(n), compute f(c),f(c+1),…,f(c+n)f(c),f(c+1),\dots ,f(c+n) modulo 998244353998244353, where 1≤n≤105,n<m≤1081\leq n\leq 10^5,n < m\leq 10^8.

Lagrange Interpolation Formula Method

Consider Lagrange interpolation formula.

f(x)=∑0≤i≤nf(i)∏0≤j≤n ∧ j≠ix−ji−j=∑0≤i≤nf(i)x!(x−n−1)!(x−i)(−1)n−ii!(n−i)!=x!(x−n−1)!∑0≤i≤nf(i)(x−i)(−1)n−ii!(n−i)!\begin{aligned} f(x)&=\sum _ {0\leq i\leq n}f(i)\prod _ {0\leq j\leq n\,\land \,j\neq i}\frac{x-j}{i-j}\\ &=\sum _ {0\leq i\leq n}f(i)\frac{x!}{(x-n-1)!(x-i)}\frac{(-1)^{n-i}}{i!(n-i)!}\\ &=\frac{x!}{(x-n-1)!}\sum _ {0\leq i\leq n}\frac{f(i)}{(x-i)}\frac{(-1)^{n-i}}{i!(n-i)!} \end{aligned}

Although the formula above has a convolution form, the denominator x−i≠0x-i\neq 0 is not always invertible. We therefore consider only the case c>nc > n below; other cases (for example, when denominators of B0(x)B_0(x) coefficients vanish modulo a prime) can be handled by case analysis. Let

A0(x)=∑0≤i≤nf(i)(−1)n−ii!(n−i)!xiB0(x)=∑i≥01c−n+ixi\begin{aligned} A_0(x)&=\sum _ {0\leq i\leq n}\frac{f(i)(-1)^{n-i}}{i!(n-i)!}x^i\\ B_0(x)&=\sum _ {i\geq 0}\frac{1}{c-n+i}x^i \end{aligned}

Then, for t≥0t\geq 0,

[xn+t](A0(x)B0(x))=∑i=0n+t([xi]A0(x))([xn+t−i]B0(x))=∑i=0nf(i)(−1)n−ii!(n−i)!1c+t−i=(c+t−n−1)!(c+t)!f(c+t)\begin{aligned} \lbrack x^{n+t}\rbrack (A_0(x)B_0(x))&=\sum _ {i=0}^{n+t}(\lbrack x^i\rbrack A_0(x))(\lbrack x^{n+t-i}\rbrack B_0(x))\\ &=\sum _ {i=0}^{n}\frac{f(i)(-1)^{n-i}}{i!(n-i)!}\frac{1}{c+t-i}\\ &=\frac{(c+t-n-1)!}{(c+t)!}f(c+t) \end{aligned}

In the implementation, truncate the required part of B0(x)B_0(x) to obtain more point values; cyclic convolution can also be used.

With a slight modification of the problem, suppose the point values of dd at f(d),f(d+k),…,f(d+nk)f(d),f(d+k),\dots ,f(d+nk) are given. We can compute f(c+d),f(c+d+k),…,f(c+d+nk)f(c+d),f(c+d+k),\dots ,f(c+d+nk), viewing them as the point values g(x)=f(d+kx)g(x)=f(d+kx) of the shifted polynomial g(0),g(1),…,g(n)g(0),g(1),\dots ,g(n) at g(c/k),g(c/k+1),…,g(c/k+n)g(c/k),g(c/k+1),\dots ,g(c/k+n).

The Lagrange interpolation formula also gives a linear-time method for computing one point value by maintaining several prefix and suffix products.

Applications

Unsigned Stirling Numbers of the First Kind in One Row

Example: P5408 — unsigned Stirling numbers of the first kind

Compute [n0],[n1],…,[nn]\displaystyle {n\brack 0},{n\brack 1},\dots ,{n\brack n} modulo the prime 167772161167772161, where 1≤n<2621441\leq n< 262144.

Consider

xn‾=∑i=0n[ni]xi,n≥0x^{\overline{n}}=\sum _ {i=0}^n{n\brack i}x^i,\quad n\geq 0

where xn‾=x⋅(x+1)⋯(x+n−1)x^{\overline{n}}=x\cdot (x+1)\cdots (x+n-1) is the rising factorial, and let fn(x)=xn‾f_n(x)=x^{\overline{n}}. Then

f2n(x)=xn‾⋅(x+n)n‾=fn(x)fn(x+n)f_{2n}(x)=x^{\overline{n}}\cdot (x+n)^{\overline{n}}=f_n(x)f_n(x+n)

Polynomial shift computes O(nlog⁡n)O(n\log n) in fn(x+n)f_n(x+n), reducing the problem to half its original size: compute the coefficients of fn(x)f_n(x). Thus

T(n)=T(n/2)+O(nlog⁡n)=O(nlog⁡n)T(n)=T(n/2)+O(n\log n)=O(n\log n)

Factorial Modulo a Prime

Example: P5282 [Template] — Fast factorial algorithm

Compute n! mod pn!\bmod p, where pp is prime and 1≤n<p≤231−11\leq n< p\leq 2^{31}-1.

Let v=⌊n⌋v=\lfloor\sqrt{n}\rfloor and g(x)=∏i=1v(x+i)g(x)=\prod _ {i=1}^v(x+i). Then

n!≡(∏i=0v−1g(iv))⋅∏i=v2+1ni(modp)n!\equiv \left(\prod _ {i=0}^{v-1}g(iv)\right)\cdot \prod _ {i=v^2+1}^n i\pmod{p}

The product ∏i=v2+1ni\prod _ {i=v^2+1}^n i can be computed in O(n)O(\sqrt{n}) time. We want to compute the first product in the formula efficiently.

Polynomial Multipoint Evaluation

The coefficients of g(x)g(x) can be computed by the polynomial-shift algorithm in O(nlog⁡n)O(n\log n), but evaluating it at the multiple points g(0),g(v),g(2v),…,g(v2−v)g(0),g(v),g(2v),\dots ,g(v^2-v) takes O(nlog⁡2n)O(\sqrt{n}\log ^2n).

Continuous Point-Value Shift

Let gd(x)=∏i=1d(x+i)g_d(x)=\prod _ {i=1}^d(x+i). We can uniquely determine this degree-dd polynomial from its d+1d+1 point values gd(0),gd(v),…,gd(dv)g_d(0),g_d(v),\dots ,g_d(dv). Also,

g2d(x)=gd(x)gd(x+d)g _ {2d}(x)=g_d(x)g_d(x+d)

Thus only 2d+12d+1 point values are needed to uniquely determine g2d(x)g _ {2d}(x). Applying continuous point-value shift to compute gd((d+1)v),gd((d+2)v),…,gd(2dv)g_d((d+1)v),g_d((d+2)v),\dots ,g_d(2dv) (that is, shift the point values h(x)=gd(vx)h(x)=g_d(vx), h(0),h(1),…,h(d)h(0),h(1),\dots ,h(d) to h(d+1),h(d+2),…,h(2d)h(d+1),h(d+2),\dots ,h(2d)) and gd(d),gd(v+d),…,gd(2dv+d)g_d(d),g_d(v+d),\dots ,g_d(2dv+d) (that is, shift the point values h(x)=gd(vx)h(x)=g_d(vx), h(0),h(1),…,h(d)h(0),h(1),\dots ,h(d) to h(d/v),h(d/v+1),h(d/v+2),…,h(d/v+2d)h(d/v),h(d/v+1),h(d/v+2),\dots ,h(d/v+2d)), then multiplying the corresponding point values gives g2d(0),g2d(v),…,g2d(2dv)g _ {2d}(0),g _ {2d}(v),\dots ,g _ {2d}(2dv).

To compute gd(0),gd(v),…,gd(dv)g_d(0),g_d(v),\dots ,g_d(dv) from gd+1(0),gd+1(v),…,gd+1(dv),gd+1((d+1)v)g _ {d+1}(0),g _ {d+1}(v),\dots ,g _ {d+1}(dv),g _ {d+1}((d+1)v), consider

gd+1(x)=(x+d+1)⋅gd(x)g _ {d+1}(x)=(x+d+1)\cdot g_d(x)

The one additional point value can be obtained by a linear-time algorithm. Therefore, starting with g1(0)=1,g1(v)=v+1g_1(0)=1,g_1(v)=v+1, we maintain these point values by repeated doubling with continuous point-value shift, obtaining

T(n)=T(n/2)+O(nlog⁡n)=O(nlog⁡n)T(n)=T(n/2)+O(n\log n)=O(n\log n)

We need only about n\sqrt{n} point values, so the time complexity is O(nlog⁡n)O(\sqrt{n}\log n).

Prefix Sums of Binomial Coefficients Modulo a Prime

Example: LOJ 6386 — prefix sums of binomial coefficients

Compute ∑i=0m(ni) mod 998244353\displaystyle \sum _ {i=0}^m\binom{n}{i}\bmod 998244353, where 0≤m≤n≤9×1080\leq m\leq n\leq 9\times 10^8.

Describe the recurrence n!=n⋅(n−1)!n!=n\cdot (n-1)! using matrices. We have

[n!]=(∏i=0n−1[i+1])[1]\begin{bmatrix} n! \end{bmatrix} =\left( \prod _ {i=0}^{n-1} \begin{bmatrix}i+1\end{bmatrix} \right) \begin{bmatrix} 1 \end{bmatrix}

Similarly, the recurrence for the prefix sum of binomial coefficients can be written as

[(nm+1)∑i=0m(ni)]=[(n−m)/(m+1)011][(nm)∑i=0m−1(ni)]\begin{bmatrix} \binom{n}{m+1}\\ \sum _ {i=0}^m\binom{n}{i} \end{bmatrix}= \begin{bmatrix} (n-m)/(m+1)&0\\ 1&1 \end{bmatrix} \begin{bmatrix} \binom{n}{m}\\ \sum _ {i=0}^{m-1}\binom{n}{i} \end{bmatrix}

Taking the order of matrix multiplication into account,

[(nm+1)∑i=0m(ni)]=(∏i=0m[(n−i)/(i+1)011])[10]=1(m+1)!(∏i=0m[n−i0i+1i+1])[10]\begin{aligned} \begin{bmatrix} \binom{n}{m+1}\\ \sum _ {i=0}^m\binom{n}{i} \end{bmatrix} &=\left( \prod _ {i=0}^{m} \begin{bmatrix} (n-i)/(i+1)&0\\1&1 \end{bmatrix} \right) \begin{bmatrix} 1\\0 \end{bmatrix}\\ &= \frac{1}{(m+1)!} \left( \prod _ {i=0}^{m} \begin{bmatrix} n-i&0\\i+1&i+1 \end{bmatrix} \right) \begin{bmatrix} 1\\0 \end{bmatrix} \end{aligned}

Let v=⌊m⌋v=\lfloor\sqrt{m}\rfloor and consider maintaining the matrix

Md(x)=∏i=1d[−x+n+1−i0x+ix+i]=[fd(x)0gd(x)hd(x)]\begin{aligned} M _ d(x)&= \prod _ {i=1}^d \begin{bmatrix} -x+n+1-i&0\\ x+i&x+i \end{bmatrix}\\ &= \begin{bmatrix} f_d(x)&0\\ g_d(x)&h_d(x) \end{bmatrix} \end{aligned}

The point values Md(0),Md(v),…,Md(dv)M _ d(0),M _ d(v),\dots ,M_d(dv), namely fd(0),fd(v),…,fd(dv)f_d(0),f_d(v),\dots ,f_d(dv), hd(0),…,hd(dv)h_d(0),\dots ,h_d(dv), and gd(0),…,gd(dv)g_d(0),\dots ,g_d(dv), together with

M2d(x)=∏i=12d[−x+n+1−i0x+ix+i]=(∏i=1d[−x−d+n+1−i0x+d+ix+d+i])(∏i=1d[−x+n+1−i0x+ix+i])=[fd(x+d)0gd(x+d)hd(x+d)][fd(x)0gd(x)hd(x)]=[fd(x+d)fd(x)0gd(x+d)fd(x)+hd(x+d)gd(x)hd(x+d)hd(x)]\begin{aligned} M _ {2d}(x)&= \prod _ {i=1}^{2d} \begin{bmatrix} -x+n+1-i&0\\ x+i&x+i \end{bmatrix}\\ &= \left( \prod _ {i=1}^d \begin{bmatrix} -x-d+n+1-i&0\\ x+d+i&x+d+i \end{bmatrix} \right) \left( \prod _ {i=1}^d \begin{bmatrix} -x+n+1-i&0\\ x+i&x+i \end{bmatrix} \right) \\ &= \begin{bmatrix} f_d(x+d)&0\\ g_d(x+d)&h_d(x+d) \end{bmatrix} \begin{bmatrix} f_d(x)&0\\ g_d(x)&h_d(x) \end{bmatrix} \\ &= \begin{bmatrix} f_d(x+d)f_d(x)&0\\ g_d(x+d)f_d(x)+h_d(x+d)g_d(x)&h_d(x+d)h_d(x) \end{bmatrix} \end{aligned}

The lower-right entry of the matrix is exactly the quantity maintained in the factorial algorithm, so

∏i=0m[n−i0i+1i+1]=(∏i=(k+1)vm[n−i0i+1i+1])[fv(kv)0gv(kv)hv(kv)]⋯[fv(0)0gv(0)hv(0)]\begin{aligned} \prod _ {i=0}^{m} \begin{bmatrix} n-i&0\\i+1&i+1 \end{bmatrix}= \left( \prod _ {i=(k+1)v}^m \begin{bmatrix} n-i&0\\ i+1&i+1 \end{bmatrix} \right) \begin{bmatrix} f_v(kv)&0\\ g_v(kv)&h_v(kv) \end{bmatrix} \cdots \begin{bmatrix} f_v(0)&0\\ g_v(0)&h_v(0) \end{bmatrix} \end{aligned}

The computation can be completed in O(mlog⁡m)O(\sqrt m\log m) time.

Harmonic Numbers Modulo a Prime

Example: P5702 — summing a harmonic series

Compute ∑i=1ni−1 mod p\sum _ {i=1}^ni^{-1}\bmod p, where pp is prime and 1≤n<p<2301\leq n< p< 2^{30}.

Let Hn=∑k=1nk−1H_n=\sum _ {k=1}^nk^{-1}. One step of the recurrence is

[(n+1)!(n+1)!Hn+1]=[n+101n+1][n!n!Hn]\begin{bmatrix} (n+1)!\\(n+1)!H _ {n+1} \end{bmatrix}= \begin{bmatrix} n+1&0\\1&n+1 \end{bmatrix} \begin{bmatrix} n!\\n!H_n \end{bmatrix}

Thus

[[n+11][n+12]]=[n!n!Hn]=(∏i=0n−1[i+101i+1])[10]\begin{bmatrix} {n+1\brack 1}\\{n+1\brack 2} \end{bmatrix}= \begin{bmatrix} n!\\n!H_n \end{bmatrix}= \left( \prod _ {i=0}^{n-1} \begin{bmatrix} i+1&0\\1&i+1 \end{bmatrix} \right) \begin{bmatrix} 1\\0 \end{bmatrix}

Here [n+11]\displaystyle {n+1\brack 1} and [n+12]\displaystyle {n+1\brack 2} are unsigned Stirling numbers of the first kind. The matrix method for maintaining point values is the same as above.

Polynomial Recurrences

For the more general case, analogous to the fast-factorial examples above, what kind of algorithm can we obtain?

Example: P6115 [Template] — Polynomial Recurrences

Given a sequence aa satisfying ∀n≥m,∑k=0man−kPk(n)=0\forall n\ge m,\sum_{k=0}^ma_{n-k}P_k(n)=0, where PkP_k is a polynomial of degree at most dd. Given the coefficients of all PkP_k and the initial values a0,a1,…,am−1a_0,a_1,\dots,a_{m-1}, find ana_n. Compute modulo 998244353998244353; n≤6×108n\le6\times10^8, 1≤m,d≤71\le m,d\le7, 7s7s, with a time limit of {11}.

To describe the matrix constructions in the examples above more systematically, we introduce the concept of lambda matrix (λ\lambda).

For polynomial recurrences, note that the point values maintained in the fast-factorial algorithm are not actually n!n!, but ∏i=0T−1(aT+i)\prod_{i=0}^{T-1}(aT+i): the ratio between a pair of point values.

Because the order of a polynomial recurrence, mm, is no longer just 11, we cannot directly maintain the ratio between two numbers. Instead, we maintain a linear transformation between a pair of mm-dimensional vectors, namely an m×mm\times m matrix; each matrix entry is the point value of a polynomial.

For a general polynomial-recurrence problem asking for a distant term, we can construct

−1P0(n)[P1(n)P2(n)P3(n)⋯Pm−1(n)Pm(n)−P0(n)−P0(n)−P0(n)⋱−P0(n)][an−1an−2an−3⋮an−m+1an−m]=[anan−1an−2⋮an−m+2an−m+1]-{\frac{1}{P_0(n)}}\begin{bmatrix}P_1(n)&P_2(n)&P_3(n)&\cdots&P_{m-1}(n)&P_m(n)\\-P_0(n)\\&-P_0(n)\\&&-P_0(n)\\&&&\ddots\\&&&&-P_0(n)\\\end{bmatrix} \begin{bmatrix}a_{n-1}\\a_{n-2}\\a_{n-3}\\\vdots\\a_{n-m+1}\\a_{n-m}\end{bmatrix} =\begin{bmatrix}a_n\\a_{n-1}\\a_{n-2}\\\vdots\\a_{n-m+2}\\a_{n-m+1}\end{bmatrix}

Let

B(λ)=[P1(λ)P2(λ)P3(λ)⋯Pm−1(λ)Pm(λ)−P0(λ)−P0(λ)−P0(λ)⋱−P0(λ)]B(\lambda)=\begin{bmatrix} P_1(\lambda)&P_2(\lambda)&P_3(\lambda)&\cdots&P_{m-1}(\lambda)&P_m(\lambda)\\ -P_0(\lambda)\\ &-P_0(\lambda)\\ &&-P_0(\lambda)\\ &&&\ddots\\ &&&&-P_0(\lambda)\\ \end{bmatrix}

Ignoring the leading factor −1P0(n)-\frac1{P_0(n)} for now, we need to maintain a quantity of the form ∏i=0T−1B(aT+m+i)\prod_{i=0}^{T-1}B(aT+m+i), where multiplication is performed from right to left.

It is easy to see that BT(λ)=∏i=0T−1B(λ+i)B_T(\lambda)=\prod_{i=0}^{T-1}B(\lambda+i) is a λ\lambda matrix whose entries have degree at most dTdT; it can be maintained using only dT+1dT+1 values.

We therefore maintain the point values of the BT(m)B_T(m) matrices BT(m+T)B_T(m+T), BT(m+2T)B_T(m+2T), …\dots, BT(m+(dT−1)T)B_T(m+(dT-1)T), BT(m+dT2)B_T(m+dT^2), and λ\lambda, and use polynomial point-value shifting and doubling in the same way as in the fast-factorial algorithm.

Specifically, to lift t=log⁡2Tt=\log_2T to 11, we proceed as follows:

  1. In O(m2dTlog⁡(dT))O(m^2dT\log(dT)) time, obtain BT(p+dT2)B_T(p+dT^2), BT(p+(dT+1)T)B_T(p+(dT+1)T), BT(p+(dT+2)T)B_T(p+(dT+2)T), ⋯\cdots, BT(p+(2dT−1)T)B_T(p+(2dT-1)T), and BT(p+(2dT)dT)B_T(p+(2dT)dT).
  2. In O(m2dTlog⁡(dT))O(m^2dT\log(dT)) time, obtain BT(p+2dT2)B_T(p+2dT^2), BT(p+(2dT+1)T)B_T(p+(2dT+1)T), BT(p+(2dT+2)T)B_T(p+(2dT+2)T), ⋯\cdots, BT(p+(3dT−1)T)B_T(p+(3dT-1)T), and BT(p+(3dT)dT)B_T(p+(3dT)dT).
  3. In O(m2dTlog⁡(dT))O(m^2dT\log(dT)) time, obtain BT(p+3dT2)B_T(p+3dT^2), BT(p+(3dT+1)T)B_T(p+(3dT+1)T), BT(p+(3dT+2)T)B_T(p+(3dT+2)T), ⋯\cdots, BT(p+(4dT−1)T)B_T(p+(4dT-1)T), and BT(p+(4dT)dT)B_T(p+(4dT)dT).
  4. Compute B2T(v)=BT(v+T)BT(v)B_{2T}(v)=B_{T}(v+T)B_{T}(v).

Each round spends O(m2dTlog⁡(dT))O(m^2dT\log(dT)) complexity on shifting. Each round performs only Θ(dT)\Theta(dT) matrix multiplication, whose complexity can be regarded as O(m3dT)O(m^3dT).

Finally, it is enough to reach T≥n/dT\ge\sqrt{n/d}.

The preceding −1P0(n)-\frac1{P_0(n)} factor can be handled similarly.

Thus the preprocessing complexity is Θ(nd(m3+m2log⁡(nd)))\Theta(\sqrt{nd}(m^3+m^2\log(nd))).

For a query, we need only Θ(n/T)\Theta(n/T) vector-matrix multiplications and O(T)O(T) brute-force transitions.

This part is clearly not the complexity bottleneck.

Therefore, the total complexity of the algorithm is Θ(nd(m3+m2log⁡(nd)))\Theta(\sqrt{nd}(m^3+m^2\log(nd))).

When coding, cyclic convolution can be used to reduce the constant factor of the NTT.

In applications, we often extract a distant coefficient of a known D-finite GF, so m,dm,d are all constants; this gives distant-coefficient evaluation in Θ(nlog⁡n)\Theta(\sqrt n\log n).

References

  • Alin Bostan, Pierrick Gaudry, and Eric Schost. Linear recurrences with polynomial coefficients and application to integer factorization and Cartier–Manin operator.
  • Min_25's blog
  • ZZQ's blog - factorial modulo a large prime