问题
给出 n n n 个点,求过这 n n n 个点的 n − 1 n-1 n−1 次函数。
待定系数法
直接根据方程求解即可(高斯消元了解一下)。
这种方法在初中叫做待定系数法,时间复杂度
Θ
(
n
3
)
\Theta(n^3)
Θ(n3)。
拉格朗日插值法
结论 令第
i
i
i 个点的坐标为
(
x
i
,
y
i
)
(x_i,y_i)
(xi,yi),且
∀
i
≠
j
,
x
i
≠
x
j
\forall i\not= j,x_i\not= x_j
∀i=j,xi=xj,则过这
n
n
n 个点的函数为
f
(
x
)
=
∑
i
=
0
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
x
−
x
j
x
i
−
x
j
\begin{aligned}f(x)=\sum_{i=0}^{n-1}y_i\prod_{j=0,j\not= i}^{n-1}\frac{x-x_j}{x_i-x_j}\end{aligned}
f(x)=i=0∑n−1yij=0,j=i∏n−1xi−xjx−xj
证明 当
0
⩽
k
<
n
0\leqslant k<n
0⩽k<n 时,有
f
(
x
k
)
=
∑
i
=
0
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
x
k
−
x
j
x
i
−
x
j
=
∑
i
=
0
,
k
≠
i
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
x
k
−
x
j
x
i
−
x
j
+
∑
i
=
0
,
k
=
i
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
x
k
−
x
j
x
i
−
x
j
=
∑
i
=
0
,
k
≠
i
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
x
k
−
x
j
x
i
−
x
j
+
y
k
∏
j
=
0
,
j
≠
i
n
−
1
x
k
−
x
j
x
k
−
x
j
=
∑
i
=
0
,
k
≠
i
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
x
k
−
x
j
x
i
−
x
j
+
y
k
∏
j
=
0
,
j
≠
i
n
−
1
1
=
∑
i
=
0
,
k
≠
i
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
x
k
−
x
j
x
i
−
x
j
+
y
k
=
∑
i
=
0
,
k
≠
i
n
−
1
y
i
∏
j
=
0
,
j
≠
i
,
j
≠
k
n
−
1
x
k
−
x
j
x
i
−
x
j
∏
j
=
0
,
j
≠
i
,
j
=
k
n
−
1
x
k
−
x
j
x
i
−
x
j
+
y
k
=
∑
i
=
0
,
k
≠
i
n
−
1
y
i
∏
j
=
0
,
j
≠
i
,
j
≠
k
n
−
1
x
k
−
x
j
x
i
−
x
j
⋅
x
j
−
x
j
x
i
−
x
j
+
y
k
=
∑
i
=
0
,
k
≠
i
n
−
1
y
i
∏
j
=
0
,
j
≠
i
,
j
≠
k
n
−
1
x
k
−
x
j
x
i
−
x
j
⋅
0
+
y
k
=
∑
i
=
0
,
k
≠
i
n
−
1
0
+
y
k
=
y
k
\begin{aligned} f(x_k)&=\sum_{i=0}^{n-1}y_i\prod_{j=0,j\not= i}^{n-1}\frac{x_k-x_j}{x_i-x_j}\\ &=\sum_{i=0,k\not= i}^{n-1}y_i\prod_{j=0,j\not= i}^{n-1}\frac{x_k-x_j}{x_i-x_j}+\sum_{i=0,k=i}^{n-1}y_i\prod_{j=0,j\not= i}^{n-1}\frac{x_k-x_j}{x_i-x_j}\\ &=\sum_{i=0,k\not= i}^{n-1}y_i\prod_{j=0,j\not= i}^{n-1}\frac{x_k-x_j}{x_i-x_j}+y_k\prod_{j=0,j\not= i}^{n-1}\frac{x_k-x_j}{x_k-x_j}\\ &=\sum_{i=0,k\not= i}^{n-1}y_i\prod_{j=0,j\not= i}^{n-1}\frac{x_k-x_j}{x_i-x_j}+y_k\prod_{j=0,j\not= i}^{n-1}1\\ &=\sum_{i=0,k\not =i}^{n-1}y_i\prod_{j=0,j\not= i}^{n-1}\frac{x_k-x_j}{x_i-x_j}+y_k\\ &=\sum_{i=0,k\not= i}^{n-1}y_i\prod_{j=0,j\not= i,j\not= k}^{n-1}\frac{x_k-x_j}{x_i-x_j}\prod_{j=0,j\not= i,j=k}^{n-1}\frac{x_k-x_j}{x_i-x_j}+y_k\\ &=\sum_{i=0,k\not= i}^{n-1}y_i\prod_{j=0,j\not= i,j\not= k}^{n-1}\frac{x_k-x_j}{x_i-x_j}\cdot\frac{x_j-x_j}{x_i-x_j}+y_k\\ &=\sum_{i=0,k\not= i}^{n-1}y_i\prod_{j=0,j\not= i,j\not= k}^{n-1}\frac{x_k-x_j}{x_i-x_j}\cdot0+y_k\\ &=\sum_{i=0,k\not= i}^{n-1}0+y_k\\ &=y_k\\ \end{aligned}
f(xk)=i=0∑n−1yij=0,j=i∏n−1xi−xjxk−xj=i=0,k=i∑n−1yij=0,j=i∏n−1xi−xjxk−xj+i=0,k=i∑n−1yij=0,j=i∏n−1xi−xjxk−xj=i=0,k=i∑n−1yij=0,j=i∏n−1xi−xjxk−xj+ykj=0,j=i∏n−1xk−xjxk−xj=i=0,k=i∑n−1yij=0,j=i∏n−1xi−xjxk−xj+ykj=0,j=i∏n−11=i=0,k=i∑n−1yij=0,j=i∏n−1xi−xjxk−xj+yk=i=0,k=i∑n−1yij=0,j=i,j=k∏n−1xi−xjxk−xjj=0,j=i,j=k∏n−1xi−xjxk−xj+yk=i=0,k=i∑n−1yij=0,j=i,j=k∏n−1xi−xjxk−xj⋅xi−xjxj−xj+yk=i=0,k=i∑n−1yij=0,j=i,j=k∏n−1xi−xjxk−xj⋅0+yk=i=0,k=i∑n−10+yk=yk
∴
∀
0
⩽
k
<
n
,
f
(
x
k
)
=
y
k
.
\therefore \forall\ 0\leqslant k<n,f(x_k)=y_k.
∴∀ 0⩽k<n,f(xk)=yk.
代码
注意,以下设求逆元的时间复杂度为
Θ
(
1
)
\Theta(1)
Θ(1)(预处理)
根据结论,可以得知,其时间复杂度为
Θ
(
n
2
)
\Theta(n^2)
Θ(n2)
long long f(long long n,long long k){
long long ans=0;
for(int i=0;i<n;i++){
long long up=y[i]%mod,dn=1;
for(int j=0;j<n;j++)
if(i!=j)
up=up*(k-x[j]+mod)%mod,dn=dn*(x[i]-x[j]+mod)%mod;
ans=(ans+up*inv(dn)%mod)%mod;
} return ans;
}
重心拉格朗日插值
优化相关。
变形
f
(
x
)
=
∑
i
=
0
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
x
−
x
j
x
i
−
x
j
=
∑
i
=
0
n
−
1
y
i
∏
j
=
0
,
j
≠
i
n
−
1
(
x
−
x
j
)
∏
j
=
0
,
j
≠
i
n
−
1
(
x
i
−
x
j
)
=
∑
i
=
0
n
−
1
y
i
∏
j
=
0
n
−
1
(
x
−
x
j
)
(
x
−
x
i
)
∏
j
=
0
,
j
≠
i
n
−
1
(
x
i
−
x
j
)
=
∏
j
=
0
n
−
1
(
x
−
x
j
)
∑
i
=
0
n
−
1
1
x
−
x
i
×
y
i
∏
j
=
0
,
j
≠
i
n
−
1
(
x
i
−
x
j
)
\begin{aligned} f(x)&=\sum_{i=0}^{n-1}y_i\prod_{j=0,j\ne i}^{n-1}\frac{x-x_j}{x_i-x_j}\\ &=\sum_{i=0}^{n-1}y_i\frac{\prod_{j=0,j\ne i}^{n-1}(x-x_j)}{\prod_{j=0,j\ne i}^{n-1}(x_i-x_j)}\\ &=\sum_{i=0}^{n-1}\frac{y_i\prod_{j=0}^{n-1}(x-x_j)}{(x-x_i)\prod_{j=0,j\ne i}^{n-1}(x_i-x_j)}\\ &=\prod_{j=0}^{n-1}(x-x_j)\sum_{i=0}^{n-1}\frac{1}{x-x_i}\times\frac{y_i}{\prod_{j=0,j\ne i}^{n-1}(x_i-x_j)}\\ \end{aligned}
f(x)=i=0∑n−1yij=0,j=i∏n−1xi−xjx−xj=i=0∑n−1yi∏j=0,j=in−1(xi−xj)∏j=0,j=in−1(x−xj)=i=0∑n−1(x−xi)∏j=0,j=in−1(xi−xj)yi∏j=0n−1(x−xj)=j=0∏n−1(x−xj)i=0∑n−1x−xi1×∏j=0,j=in−1(xi−xj)yi
令
h
(
x
)
=
∏
j
=
0
n
−
1
(
x
−
x
j
)
,
w
i
=
y
i
∏
j
=
0
,
j
≠
i
n
−
1
(
x
i
−
x
j
)
\begin{aligned}h(x)=\prod_{j=0}^{n-1}(x-x_j),w_i=\dfrac{y_i}{\prod_{j=0,j\ne i}^{n-1}(x_i-x_j)}\end{aligned}
h(x)=j=0∏n−1(x−xj),wi=∏j=0,j=in−1(xi−xj)yi,则
f
(
x
)
=
h
(
x
)
∑
i
=
0
n
−
1
w
i
x
−
x
i
\begin{aligned}f(x)=h(x)\sum_{i=0}^{n-1}\dfrac{w_i}{x-x_i}\end{aligned}
f(x)=h(x)i=0∑n−1x−xiwi
求值 这样单次求值的时间复杂度仍为
Θ
(
n
2
)
\Theta(n^2)
Θ(n2)。
动态加点 更新
h
h
h 是
Θ
(
1
)
\Theta(1)
Θ(1) 的,因此只需要
Θ
(
n
)
\Theta(n)
Θ(n) 更新
w
i
(
1
⩽
i
<
n
)
w_i(1\leqslant i<n)
wi(1⩽i<n) 并用
Θ
(
n
)
\Theta(n)
Θ(n) 的时间计算
w
n
w_n
wn 即可。