多项式插值

多项式插值

Keep away from polynomial. ---- Wild_Donkey

Published on 2022-02-20, Updated on 2022-07-02

(x0,y0),(x1,y1),...,(xn,yn)(x_0, y_0), (x_1, y_1),...,(x_n, y_n), 共 n+1n + 1 个点. 求一个 nnn+1n + 1 项的多项式 LL, 使得多项式的图像过每一个点. 这个多项式 LL 便是拉格朗日多项式 (Lagrange polynomial).

拉格朗日基本多项式 (插值基函数)

i\ell_i 是一个多项式, 满足 (xi)=1\ell(x_i) = 1, 且 (xj)=0 (ji)\ell(x_j) = 0\ (j \neq i).

i(x)=j=0,jinxxjxixj\ell_i(x) = \prod_{j = 0, j \neq i}^{n} \frac{x - x_j}{x_i - x_j}

这样当 x=xix = x_i 时, 所有 xxjxixj\dfrac{x - x_j}{x_i - x_j} 的分子等于分母. 当 xxix \neq x_i 时, 一定存在一个项分子为 00, 所以乘起来还是 00.

拉格朗日多项式

因为每个拉格朗日基本多项式 i\ell_i 在除了 xix_i 点以外的给定的横坐标上都是 00, 所以我们把所有拉格朗日多项式加权求和就能得到符合要求的多项式 LL.

L(x)=i=0nyii(x)L(x)=i=0nyij=0,jinxxjxixjL(x) = \sum_{i = 0}^{n} y_i\ell_i(x) L(x) = \sum_{i = 0}^{n} y_i\prod_{j = 0, j \neq i}^{n} \frac{x - x_j}{x_i - x_j}

重心拉格朗日插值法

为了方便计算, 预处理一个 n+1n + 1 次多项式 \ell:

(x)=i=0n(xxi)\ell(x) = \prod_{i = 0}^n (x - x_i)

这个多项式可以进行 O(n)O(n) 次耗时 O(n)O(n)O(n)O(n) 项式和二项式乘法得到, 总耗时 O(n2)O(n^2).

定义重心权 wiw_i:

wi=1j=0,jin(xixj)w_i = \frac 1{\displaystyle{\prod_{j = 0, j \neq i}^n (x_i - x_j)}}

这样就可以得到:

i(x)=(x)wixxiL(x)=i=0n(x)wiyixxiL(x)=(x)i=0nwiyixxi\begin{aligned} \ell_i(x) &= \frac{\ell(x)w_i}{x - x_i}\\ L(x) &= \sum_{i = 0}^{n} \frac{\ell(x)w_iy_i}{x - x_i}\\ L(x) &= \ell(x) \sum_{i = 0}^{n} \frac{w_iy_i}{x - x_i}\\ \end{aligned}

我们可以 O(n)O(n) 地算出 wiyiw_iy_i, 然后 O(n)O(n) 地算出 (x)xxi\dfrac {\ell(x)}{x - x_i}, 在除法过程中就可以直接统计 i\ell_iLL 的贡献. 所以在预处理之后, 计算 L(x)L(x) 需要 O(n2)O(n^2) 的时间.

代码实现

模板题

代码还是很好理解的, 只需要把前面的式子用程序实现就可以了.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const unsigned long long Mod(998244353);
unsigned long long Pnt[2005][2], La[2005], L[2005], Ans(0), m, Now(1);
unsigned n;
unsigned A, B, C, D, t;
unsigned Cnt(0);
inline unsigned long long Inv(unsigned long long x) {
unsigned long long Rt(1);
unsigned y(998244351);
while (y) { if (y & 1) Rt = Rt * x % Mod; x = x * x % Mod, y >>= 1; }
return Rt;
}
signed main() {
n = RD(), m = RD();
for (unsigned i(0); i < n; ++i) Pnt[i][0] = RD(), Pnt[i][1] = RD();
La[1] = 1, La[0] = Mod - Pnt[0][0];
for (unsigned i(1); i < n; ++i) {
for (unsigned j(i + 1); j; --j)
La[j] = (La[j] * (Mod - Pnt[i][0]) + La[j - 1]) % Mod;
La[0] = La[0] * (Mod - Pnt[i][0]) % Mod;
}
for (unsigned i(0); i < n; ++i) {
unsigned long long Mul(1), Tmp(La[n]);
for (unsigned j(0); j < n; ++j) if (j ^ i) Mul = Mul * (Mod + Pnt[i][0] - Pnt[j][0]) % Mod;
Mul = Inv(Mul) * Pnt[i][1] % Mod;
for (unsigned j(n - 1); ~j; --j) {
L[j] = (L[j] + Mul * Tmp) % Mod;
Tmp = (La[j] + Tmp * Pnt[i][0]) % Mod;
}
}
for (unsigned i(0); i < n; ++i) {
Ans = (Ans + L[i] * Now) % Mod;
Now = Now * m % Mod;
}
printf("%llu\n", Ans);
return Wild_Donkey;
}

加点

如果我们需要对已经插值的 nn 个点加入一个点, 如果重新插值, 需要 O(n2)O(n^2) 的时间.

考虑插入一个点 (xn+1,yn+1)(x_{n + 1}, y_{n + 1})L(x)L(x) 的影响, 它会使插入后 (x)=(x)(xxn+1)\ell'(x) = \ell(x)(x - x_{n + 1}). 会使每个满足 ini \leq nwiw_i 都乘以 1xixn+1\frac 1{x_i - x_{n + 1}}. 还会使 L(x)L(x) 加上 (x)wn+1yn+1xxn+1\frac{\ell'(x)w_{n + 1}y_{n + 1}}{x - x_{n + 1}}.

L(x)=(x)(i=0n+1wiyixxi)L'(x) = \ell'(x) (\sum_{i = 0}^{n + 1} \frac{w_i'y_i}{x - x_i})

我们用 O(n)O(n) 处理出 wi (in)w_i'\ (i \leq n), 然后 O(n)O(n) 直接算出 wn+1w_{n + 1}'. 我们可以在 O(n)O(n) 的时间内做乘法得到 (x)\ell'(x). 维护了 ww 数组和多项式 (x)\ell(x) 就实现了对 L(x)L(x) 的维护. 需要时 O(n2)O(n^2) 可以求出多项式 L(x)L(x).

如果问题只需要维护多项式 L(x)L(x) 的一个点值 yy, 那么问题就更简单了, 我们可以直接维护 (x)\ell(x) 的点值, 这样就可以把多项式除法变成整数乘除, 实现直接维护 yy 并实现 O(n)O(n) 插入.

第二型重心拉格朗日插值法

现在有另一组点, 每个点和我们要插值的点一一对应, 每一对点的横坐标相同, 而新给出的这些点纵坐标是 11. 我们对新点进行插值, 得到多项式 g(x)g(x), 无论 xx 取什么值, g(x)=1g(x) = 1. 如果我们代入拉格朗日插值公式, 可以得到:

g(x)=(x)i=0nwixxig(x) = \ell(x) \sum_{i = 0}^{n} \frac{w_i}{x - x_i}

因为任何数除以 11 还是它本身, 所以 L(x)=L(x)g(x)L(x) = \dfrac{L(x)}{g(x)}. 将 L(x)L(x), g(x)g(x) 都用拉格朗日插值公式表示, 则得到:

L(x)=L(x)g(x)=(x)i=0nwiyixxi(x)i=0nwixxi=i=0nwiyixxii=0nwixxi\begin{aligned} L(x) &= \frac{L(x)}{g(x)}\\ &= \frac {\ell(x) \displaystyle{\sum_{i = 0}^{n} \frac{w_iy_i}{x - x_i}}}{\ell(x) \displaystyle{\sum_{i = 0}^{n} \frac{w_i}{x - x_i}}}\\ &= \frac {\displaystyle{\sum_{i = 0}^{n} \frac{w_iy_i}{x - x_i}}}{\displaystyle{\sum_{i = 0}^{n} \frac{w_i}{x - x_i}}}\\ \end{aligned}

我们可以只维护 ww 数组, 实现 O(n)O(n) 求值, O(n)O(n) 插入.

差商/均差 (Divided Diffrences)

对于点 (xi,yi),(xi+1,yi+1),...,(xi+j,yi+j)(x_i, y_i), (x_{i + 1}, y_{i + 1}),...,(x_{i + j}, y_{i + j}), 用 [y][y_]

对于函数 f(x)f(x), 用 f[xi,xi+1,...,xi+j]f[x_i, x_{i + 1},...,x_{i + j}] 表示函数的点 (xi,f(xi)),(xi+1,f(xi+1)),...,(xi+j,f(xi+j))(x_i, f(x_i)), (x_{i + 1}, f(x_{i + 1})),..., (x_{i + j}, f(x_{i + j})) 的均差.

均差的预算规则是这样的:

单点的均差就是这个点的纵坐标: f[xi]=f(xi)f[x_i] = f(x_i)

多点均差有递归定义: f[xi,xi+1,...,xi+j]=f[xi+1,...,xi+j]f[xi,...,xi+j1]xi+jxif[x_i, x_{i + 1},...,x_{i + j}] = \dfrac{f[x_{i + 1},...,x_{i + j}] - f[x_i,...,x_{i + j - 1}]}{x_{i + j} - x_{i}}

均差有前向和后向之分, 一般只会用到前向均差, 后向均差是把元素顺序倒过来写, 计算上也有一部分需要翻转.

我们可以递推地在 O(n2)O(n^2) 的时间内求出 nn 个点的序列的每个子串的均差.

均差的展开形式是这样的:

f[xl,xl+1,...,xr]=i=lrf(xi)j=l,jirxixjf[x_l, x_{l + 1},...,x_{r}] = \sum_{i = l}^{r} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l, j \neq i}^{r} x_i - x_j}}

用归纳法证明, 单点的均差符合 f[xl]=f(xl)1=f(xl)f[x_l] = \dfrac{f(x_l)}{1} = f(x_l). 假设大小小于 rl+1r - l + 1 的点集都满足这个式子, 验证大小为 rl+1r - l + 1 的点集是否满足.

f[xl,xl+1,...,xr]=f[xi+1,...,xi+j]f[xi,...,xi+j1]xi+jxif[xl,xl+1,...,xr]=i=l+1rf(xi)j=l+1,jirxixji=lr1f(xi)j=l,jir1xixjxrxlf[xl,xl+1,...,xr]=i=l+1r1f(xi)j=l+1,jirxixji=l+1r1f(xi)j=l,jir1xixj+f(xr)j=l+1r1xrxjf(xl)j=l+1r1xlxjxrxlf[xl,xl+1,...,xr]=i=l+1r1f(xi)j=l+1,jirxixji=l+1r1f(xi)j=l,jir1xixjxrxl+f(xr)j=lr1xrxj+f(xl)j=l+1rxlxjf[xl,xl+1,...,xr]=i=l+1r1f(xi)j=l+1,jirxixjf(xi)j=l,jir1xixjxrxl+f(xr)j=lr1xrxj+f(xl)j=l+1rxlxjf[xl,xl+1,...,xr]=i=l+1r1f(xi)xixrf(xi)xixlj=l+1,jir1xixjxrxl+f(xr)j=lr1xrxj+f(xl)j=l+1rxlxjf[xl,xl+1,...,xr]=i=l+1r1f(xi)((xixl)(xixr))(xixr)(xixl)j=l+1,jir1xixjxrxl+f(xr)j=lr1xrxj+f(xl)j=l+1rxlxjf[xl,xl+1,...,xr]=i=l+1r1f(xi)(xrxl)(xixr)(xixl)j=l+1,jir1xixjxrxl+f(xr)j=lr1xrxj+f(xl)j=l+1rxlxjf[xl,xl+1,...,xr]=i=l+1r1f(xi)(xixr)(xixl)j=l+1,jir1xixj+f(xr)j=lr1xrxj+f(xl)j=l+1rxlxjf[xl,xl+1,...,xr]=i=l+1r1f(xi)j=l,jirxixj+f(xr)j=lr1xrxj+f(xl)j=l+1rxlxjf[xl,xl+1,...,xr]=i=lrf(xi)j=l,jirxixj\begin{aligned} f[x_l, x_{l + 1},...,x_{r}] &= \dfrac{f[x_{i + 1},...,x_{i + j}] - f[x_i,...,x_{i + j - 1}]}{x_{i + j} - x_{i}}\\ f[x_l, x_{l + 1},...,x_{r}] &= \frac{\displaystyle{\sum_{i = l + 1}^{r} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l + 1, j \neq i}^{r} x_i - x_j}} - \sum_{i = l}^{r - 1} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l, j \neq i}^{r - 1} x_i - x_j}}}}{x_r - x_l}\\ f[x_l, x_{l + 1},...,x_{r}] &= \frac{\displaystyle{\sum_{i = l + 1}^{r - 1} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l + 1, j \neq i}^{r} x_i - x_j}} - \sum_{i = l + 1}^{r - 1} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l, j \neq i}^{r - 1} x_i - x_j}} + \dfrac{f(x_r)}{\displaystyle{\prod_{j = l + 1}^{r - 1} x_r - x_j}} - \dfrac{f(x_l)}{\displaystyle{\prod_{j = l + 1}^{r - 1} x_l - x_j}}}}{x_r - x_l}\\ f[x_l, x_{l + 1},...,x_{r}] &= \frac{\displaystyle{\sum_{i = l + 1}^{r - 1} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l + 1, j \neq i}^{r} x_i - x_j}} - \sum_{i = l + 1}^{r - 1} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l, j \neq i}^{r - 1} x_i - x_j}}}}{x_r - x_l} + \dfrac{f(x_r)}{\displaystyle{\prod_{j = l}^{r - 1} x_r - x_j}} + \dfrac{f(x_l)}{\displaystyle{\prod_{j = l + 1}^{r} x_l - x_j}}\\ f[x_l, x_{l + 1},...,x_{r}] &= \frac{\displaystyle{\sum_{i = l + 1}^{r - 1} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l + 1, j \neq i}^{r} x_i - x_j}} - \dfrac{f(x_i)}{\displaystyle{\prod_{j = l, j \neq i}^{r - 1} x_i - x_j}}}}{x_r - x_l} + \dfrac{f(x_r)}{\displaystyle{\prod_{j = l}^{r - 1} x_r - x_j}} + \dfrac{f(x_l)}{\displaystyle{\prod_{j = l + 1}^{r} x_l - x_j}}\\ f[x_l, x_{l + 1},...,x_{r}] &= \frac{\displaystyle{\sum_{i = l + 1}^{r - 1} \dfrac{\dfrac{f(x_i)}{x_i - x_r} - \dfrac{f(x_i)}{x_i - x_l}}{\displaystyle{\prod_{j = l + 1, j \neq i}^{r - 1} x_i - x_j}}}}{x_r - x_l} + \dfrac{f(x_r)}{\displaystyle{\prod_{j = l}^{r - 1} x_r - x_j}} + \dfrac{f(x_l)}{\displaystyle{\prod_{j = l + 1}^{r} x_l - x_j}}\\ f[x_l, x_{l + 1},...,x_{r}] &= \frac{\displaystyle{\sum_{i = l + 1}^{r - 1} \dfrac{\dfrac{f(x_i)((x_i - x_l) - (x_i - x_r))}{(x_i - x_r)(x_i - x_l)}}{\displaystyle{\prod_{j = l + 1, j \neq i}^{r - 1} x_i - x_j}}}}{x_r - x_l} + \dfrac{f(x_r)}{\displaystyle{\prod_{j = l}^{r - 1} x_r - x_j}} + \dfrac{f(x_l)}{\displaystyle{\prod_{j = l + 1}^{r} x_l - x_j}}\\ f[x_l, x_{l + 1},...,x_{r}] &= \frac{\displaystyle{\sum_{i = l + 1}^{r - 1} \dfrac{\dfrac{f(x_i)(x_r - x_l)}{(x_i - x_r)(x_i - x_l)}}{\displaystyle{\prod_{j = l + 1, j \neq i}^{r - 1} x_i - x_j}}}}{x_r - x_l} + \dfrac{f(x_r)}{\displaystyle{\prod_{j = l}^{r - 1} x_r - x_j}} + \dfrac{f(x_l)}{\displaystyle{\prod_{j = l + 1}^{r} x_l - x_j}}\\ f[x_l, x_{l + 1},...,x_{r}] &= \sum_{i = l + 1}^{r - 1} \dfrac{\dfrac{f(x_i)}{(x_i - x_r)(x_i - x_l)}}{\displaystyle{\prod_{j = l + 1, j \neq i}^{r - 1} x_i - x_j}} + \dfrac{f(x_r)}{\displaystyle{\prod_{j = l}^{r - 1} x_r - x_j}} + \dfrac{f(x_l)}{\displaystyle{\prod_{j = l + 1}^{r} x_l - x_j}}\\ f[x_l, x_{l + 1},...,x_{r}] &= \sum_{i = l + 1}^{r - 1} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l, j \neq i}^{r} x_i - x_j}} + \dfrac{f(x_r)}{\displaystyle{\prod_{j = l}^{r - 1} x_r - x_j}} + \dfrac{f(x_l)}{\displaystyle{\prod_{j = l + 1}^{r} x_l - x_j}}\\ f[x_l, x_{l + 1},...,x_{r}] &= \sum_{i = l}^{r} \dfrac{f(x_i)}{\displaystyle{\prod_{j = l, j \neq i}^{r} x_i - x_j}}\\ \end{aligned}

展开式得证.

牛顿基本多项式

设多项式 ni(x)n_i(x) 满足在 x0,x1,...,xj1x_0, x_1,...,x_{j - 1} 处都取 00 的多项式. 显然有:

ni(x)=j=0i1(xxj)n_i(x) = \prod_{j = 0}^{i - 1} (x - x_j)

牛顿多项式

我们只要给每个牛顿基本多项式加权求和, 就能构造一个 nn 次多项式, 过全部 n+1n + 1 个点. 这个总和便是牛顿多项式 N(x)N(x).

假设给 ni(x)n_i(x) 加的权为 aia_i, 写出定义式:

N(x)=i=0naini(x)=i=0naij=0i1(xxj)\begin{aligned} N(x) &= \sum_{i = 0}^{n} a_in_i(x)\\ &= \sum_{i = 0}^{n}a_i\prod_{j = 0}^{i - 1} (x - x_j) \end{aligned}

因为 n+1n + 1 个横坐标不同的点可以唯一地确定一个 nn 次多项式, 因此相同点集满足 L(x)=N(x)L(x) = N(x). 我们可以用拉格朗日插值公式来求 aa. 已经求出已经插了 (x0,y0),...,(xn1,yn1)(x_0, y_0),...,(x_{n - 1}, y_{n - 1}) 的拉格朗日多项式 L(x)L(x). 我们尝试给 ana_n 赋值, 使得 L(xn)+annn(xn)L(x_n) + a_nn_n(x_n)yny_n.

yn=L(xn)+annn(xn)yn=i=0n1yij=0,jin1xnxjxixj+ani=0n1(xnxi)ani=0n1(xnxi)=yni=0n1yij=0,jin1xnxjxixjan=yni=0n1(xnxi)i=0n1yixnxij=0,jin11xixjan=yni=0n1(xnxi)+i=0n1yixixnj=0,jin11xixjan=yni=0n1(xnxi)+i=0n1yij=0,jin1xixjan=yni=0n1(xnxi)+i=0n1yij=0,jinxixjan=i=0nyij=0,jinxixj\begin{aligned} y_n &= L(x_n) + a_nn_n(x_n)\\ y_n &= \sum_{i = 0}^{n - 1} y_i\prod_{j = 0, j \neq i}^{n - 1} \frac{x_n - x_j}{x_i - x_j} + a_n\prod_{i = 0}^{n - 1} (x_n - x_i)\\ a_n\prod_{i = 0}^{n - 1} (x_n - x_i) &= y_n - \sum_{i = 0}^{n - 1} y_i\prod_{j = 0, j \neq i}^{n - 1} \frac{x_n - x_j}{x_i - x_j}\\ a_n &= \dfrac{y_n}{\displaystyle{\prod_{i = 0}^{n - 1} (x_n - x_i)}} - \sum_{i = 0}^{n - 1} \frac{y_i}{x_n - x_i} \prod_{j = 0, j \neq i}^{n - 1} \frac 1{x_i - x_j}\\ a_n &= \dfrac{y_n}{\displaystyle{\prod_{i = 0}^{n - 1} (x_n - x_i)}} + \sum_{i = 0}^{n - 1} \frac{y_i}{x_i - x_n} \prod_{j = 0, j \neq i}^{n - 1} \frac 1{x_i - x_j}\\ a_n &= \dfrac{y_n}{\displaystyle{\prod_{i = 0}^{n - 1} (x_n - x_i)}} + \sum_{i = 0}^{n - 1} y_i \prod_{j = 0, j \neq i}^{n} \frac 1{x_i - x_j}\\ a_n &= \dfrac{y_n}{\displaystyle{\prod_{i = 0}^{n - 1} (x_n - x_i)}} + \sum_{i = 0}^{n - 1} \frac {y_i}{\displaystyle {\prod_{j = 0, j \neq i}^{n} x_i - x_j}}\\ a_n &= \sum_{i = 0}^{n} \frac {y_i}{\displaystyle {\prod_{j = 0, j \neq i}^{n} x_i - x_j}}\\ \end{aligned}

我们发现 an=i=0nyij=0,jinxixj=[y0,y1,...,yn]a_n = \displaystyle{\sum_{i = 0}^{n} \frac {y_i}{\displaystyle {\prod_{j = 0, j \neq i}^{n} x_i - x_j}}} = [y_0, y_1,...,y_n].

所以牛顿多项式便是:

N(x)=i=0naini(x)=i=0naij=0i1(xxj)=i=0n[y0,y1,...,yi]j=0i1(xxj)=i=0n(j=0iyjk=0,kjixjxk)j=0i1(xxj)\begin{aligned} N(x) &= \sum_{i = 0}^{n} a_in_i(x)\\ &= \sum_{i = 0}^{n}a_i\prod_{j = 0}^{i - 1} (x - x_j)\\ &= \sum_{i = 0}^{n}[y_0, y_1,...,y_i]\prod_{j = 0}^{i - 1} (x - x_j)\\ &= \sum_{i = 0}^{n} \Big(\displaystyle{\sum_{j = 0}^{i} \frac {y_j}{\displaystyle {\prod_{k = 0, k \neq j}^{i} x_j - x_k}}\Big)}\prod_{j = 0}^{i - 1} (x - x_j)\\ \end{aligned}

横坐标为连续正整数时的线性插值

尝试化简横坐标为连续正整数的拉格朗日多项式:

j=0,jin(ij)=(j=0i1(ij))(j=i+1n(ij))=(j=0i1(ij))(j=i+1n(ji))(1)ni=i!(ni)!(1)ni\begin{aligned} \prod_{j = 0, j \neq i}^{n} (i - j) &= \Big(\prod_{j = 0}^{i - 1} (i - j)\Big)\Big(\prod_{j = i + 1}^n (i - j)\Big)\\ &= \Big(\prod_{j = 0}^{i - 1} (i - j)\Big)\Big(\prod_{j = i + 1}^n (j - i)\Big) (-1)^{n - i}\\ &= i!(n - i)!(-1)^{n - i}\\ \end{aligned}

xi=j=0i1(xj)x^{\underline{i}} = \prod_{j = 0}^{i - 1} (x - j), 这种情况的拉格朗日多项式可以改写为:

L(x)=i=0nx!f(i)(xn1)!i!(ni)!(1)ni(xi)=xn+1i=0nf(i)i!(ni)!(1)ni(xi)\begin{aligned} L(x) &= \sum_{i = 0}^{n} \frac{x!f(i)}{(x - n - 1)!i!(n - i)!(-1)^{n - i}(x - i)}\\ &= x^{\underline{n + 1}} \sum_{i = 0}^{n} \frac{f(i)}{i!(n - i)!(-1)^{n - i}(x - i)} \end{aligned}

可以发现这个时候如果只需要求一个点值, 可以直接线性求出, 求多个点值也会有很优秀的性质, 接下来用两道题来分别实现一下单点插值和多点插值的情况.

CF622F The Sum of the k-th Powers

求前 nn 个正整数的 kk 次方和对 109+710^9 + 7 取模的结果. (n109,k106)(n \leq 10^9, k \leq 10^6)

nk+1n \leq k + 1 的时候, 我们可以直接快速幂在 O(nlogk)O(n\log k) 的时间内得到答案. 我们知道自然数的 kk 次方和公式是一个 k+1k + 1 次的多项式, 所以在 n>k+1n > k + 1 的时候, 我们只要想办法插出这个多项式直接求 x=nx = n 的值即可. 我们可以自己选择点值的横坐标, 前面已经推出来, 拉格朗日多项式在点的横坐标为连续的自然数的时候是这样的形式:

L(x)=xn+1i=0niki!(ni)!(1)ni(xi)L(x)=i=0nikxi(xn)nii!(ni)!(1)ni\begin{aligned} L(x) &= x^{\underline{n + 1}}\sum_{i = 0}^{n}\frac {i^k}{i!(n - i)!(-1)^{n - i}(x - i)}\\ L(x) &= \sum_{i = 0}^{n}\frac {i^kx^{\underline{i}}(x - n)^{\overline{n - i}}}{i!(n - i)!(-1)^{n - i}} \end{aligned}

因此 O(klogk)O(k\log k) 求出一些点值, 线性求出 L(k)L(k) 即可.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const unsigned long long Mod(1000000007);
unsigned a[1000005], FacInv[1000005], Inv[1000005], Up[1000005], Down[1000005], m, n;
unsigned A, B, C, D, t;
unsigned long long Ans(0);
inline void Mn(unsigned& x) { x -= ((x >= Mod) ? Mod : 0); }
inline void Mn(unsigned long long& x) { x -= ((x >= Mod) ? Mod : 0); }
inline unsigned long long Pow(unsigned long long x, unsigned y) {
unsigned long long Rt(1);
while (y) { if (y & 1) Rt = Rt * x % Mod; x = x * x % Mod, y >>= 1; }
return Rt;
}
signed main() {
n = RD(), m = RD();
if (n <= (m + 1)) {
for (unsigned i(1); i <= n; ++i) Mn(Ans += Pow(i, m));
printf("%llu\n", Ans);
return Wild_Donkey;
}
for (unsigned i(0); i <= m; ++i) Mn(a[i + 1] = a[i] + Pow(i + 1, m));
++m, Up[0] = Down[0] = FacInv[0] = Inv[0] = Inv[1] = 1;
for (unsigned long long i(0); i < m; ++i) Up[i + 1] = Up[i] * (n - m + i) % Mod;
for (unsigned long long i(0); i < m; ++i) Down[i + 1] = Down[i] * (n - i) % Mod;
for (unsigned i(2); i <= m; ++i) Inv[i] = Inv[Mod % i] * (Mod - (Mod / i)) % Mod;
for (unsigned i(1); i <= m; ++i) FacInv[i] = (unsigned long long)FacInv[i - 1] * Inv[i] % Mod;
for (unsigned i(m); i < 0x3f3f3f3f; i -= 2)
Ans = (Ans + ((((unsigned long long)Down[i] * Up[m - i] % Mod) * FacInv[i] % Mod) * FacInv[m - i] % Mod) * a[i]) % Mod;
for (unsigned i(m - 1); i < 0x3f3f3f3f; i -= 2)
Ans = (Ans + ((((unsigned long long)Down[i] * Up[m - i] % Mod) * FacInv[i] % Mod) * FacInv[m - i] % Mod) * (Mod - a[i])) % Mod;
printf("%llu\n", Ans);
return Wild_Donkey;
}

例题

有一个 nn 次以内的多项式 f(x)f(x), 给 n+1n + 1 个点值, f(0),f(1),...,f(n)f(0), f(1),..., f(n). 求 f(m),f(m+1),...,f(m+n)f(m), f(m + 1),..., f(m + n).

这个题 O(n2)O(n^2) 无法通过, 而且由于询问也是 n+1n + 1 个点值, 所以插出系数来也无法用正确的复杂度询问. 发现横坐标很特殊, xi=ix_i = i, 询问也很特殊, 横坐标也是连续的, 所以这时拉格朗日多项式是这样的:

L(x)=xn+1i=0nf(i)i!(ni)!(1)ni(xi)L(x) = x^{\underline{n + 1}}\sum_{i = 0}^{n}\frac {f(i)}{i!(n - i)!(-1)^{n - i}(x - i)}

我们可以 O(n)O(n) 处理 f(i)i!(ni)!(1)ni\dfrac{f(i)}{i!(n - i)!(-1)^{n - i}}, 和对于所有 xxxn+1x^{\underline{n + 1}}.

但是我们要想计算一个点值, 仍然需要 O(n)O(n) 枚举 ii.

定义序列 Ai=f(i)i!(ni)!(1)niA_i = \dfrac{f(i)}{i!(n - i)!(-1)^{n - i}}, Bi=1m+inB_i = \dfrac 1{m + i - n}, Ci=j=0iAjBijC_i = \displaystyle \sum_{j = 0}^{i} A_jB_{i - j}, 三个序列长度为 2(n+1)2(n + 1), Ai=0 (i>n)A_i = 0\ (i > n).

L(x)=xn+1Cxm+nL(x) = x^{\underline{n + 1}}C_{x - m + n}

我们用 O(nlogn)O(n\log n) 卷积算出 CC, 然后就可以 O(1)O(1) 查询点值.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const unsigned long long Mod(998244353);
unsigned long long C1[160005], IFac[160005], MFac[160005], A[530000], B[530000];
unsigned long long Tmp(1), w;
unsigned m, n, N(1);
unsigned D, t;
unsigned Cnt(0), Ans(0);
inline void Mn(unsigned long long& x) { x -= ((x >= Mod) ? Mod : 0); }
inline unsigned long long W(unsigned x) {
unsigned long long Now(3), Rt(1);
while (x) { if (x & 1) Rt = Rt * Now % Mod; Now = Now * Now % Mod, x >>= 1; }
return Rt;
}
inline unsigned long long Inv(unsigned long long x) {
unsigned long long Rt(1);
unsigned y(998244351);
while (y) { if (y & 1) Rt = Rt * x % Mod;y >>= 1, x = x * x % Mod; }
return Rt;
}
inline void DIT(unsigned long long* f) {
unsigned long long Now(1), Tmpw[20];
unsigned Lg(0);
Tmpw[0] = w;
for (unsigned i(1); i < N; i <<= 1, ++Lg) Tmpw[Lg + 1] = Tmpw[Lg] * Tmpw[Lg] % Mod; --Lg;
for (unsigned i(1); i < N; i <<= 1, --Lg) {
for (unsigned j(0); j < N; ++j, Now = Now * Tmpw[Lg] % Mod) if (!(i & j)) {
unsigned long long TmpA(f[j]), TmpB(f[j ^ i] * Now % Mod);
f[j] = TmpA + TmpB, Mn(f[j]);
f[j ^ i] = Mod + TmpA - TmpB, Mn(f[j ^ i]);
}
}
}
inline void DIF(unsigned long long* f) {
unsigned long long Now(1);
for (unsigned i(N >> 1); i; i >>= 1, w = w * w % Mod) {
for (unsigned j(0); j < N; ++j, Now = Now * w % Mod) if (!(i & j)) {
unsigned long long TmpA(f[j]), TmpB(f[j ^ i]);
f[j] = TmpA + TmpB, Mn(f[j]);
f[j ^ i] = (Mod + TmpA - TmpB) * Now % Mod;
}
}
}
signed main() {
n = RD(), m = RD();
while (N <= n) { N <<= 1; } N <<= 1;
for (unsigned i(0); i <= n; ++i) A[i] = RD();
for (unsigned i(1); i <= n; ++i) Tmp = Tmp * i % Mod;
IFac[n] = Inv(Tmp);
for (unsigned i(n - 1); ~i; --i) IFac[i] = IFac[i + 1] * (i + 1) % Mod;
for (unsigned i(0); i <= n; ++i)
A[i] = ((((n - i) & 1) ? (Mod - A[i]) : A[i]) * IFac[n - i] % Mod) * IFac[i] % Mod;
C1[0] = 1;
for (unsigned i(m - n); i <= m; ++i) C1[0] = C1[0] * i % Mod;
for (unsigned i(1); i <= n; ++i) C1[i] = (C1[i - 1] * Inv(m - n + i - 1) % Mod) * (m + i) % Mod;
for (unsigned i(n << 1); ~i; --i) B[i] = Inv(m + i - n);
w = W(998244352 / N), DIF(A), w = W(998244352 / N), DIF(B);
for (unsigned i(0); i < N; ++i) A[i] = A[i] * B[i] % Mod;
w = Inv(W(998244352 / N)), DIT(A), w = Inv(N), N = (n << 1);
for (unsigned i(n); i <= N; ++i) printf("%llu ", (C1[i - n] * A[i] % Mod) * w % Mod); putchar(0x0A);
return Wild_Donkey;
}