# 简介

$F(x) = \sum_{i=1}^{k+1}y_i\prod_{i\not ={j}}\dfrac{x-x_j}{x_i-x_j}$

# 正确性

$$k + 1$$ 个点值代入即可检验。
$$x=x_k$$ 时，对 $$i$$ 进行讨论：

1. $$i\not ={k}$$ 时，存在一个 $$j$$ 满足 $$j=k$$
对于乘积项的分子 $$\prod\limits_{i\not ={j}}x-x_j$$，当 $$j=k$$ 时，$$x-x_j = 0$$
对答案无贡献。
2. $$i=k$$ 时，乘积项变为 $$\prod\limits_{i\not ={j}}\dfrac{x_i-x_j}{x_i-x_j}$$
其值恒等于 $$1$$

# 模板题

P4781 【模板】拉格朗日插值

//
/*
By:Luckyblock
*/
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#define ll long long
const int MARX = 2010;
const ll Mod = 998244353;
//=============================================================
ll ans, N, K, X[MARX], Y[MARX];
//=============================================================
inline int read()
{
int f = 1, w = 0; char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) w = (w << 3) + (w << 1) + (ch ^ '0');
return f * w;
}
ll qpow(ll x, ll y, ll mod)
{
ll ret = 1;
for(; y; x = x * x % mod, y >>= 1)
if(y & 1) ret = ret * x % mod;
return ret;
}
//=============================================================
int main()
{
N = read(), K = read();
for(int i = 1; i <= N; i ++) X[i] = read(), Y[i] = read();
for(int i = 1; i <= N; i ++)
{
ll tmp = 1ll;
for(int j = 1; j <= N; j ++)
if(i != j) tmp = tmp * (X[i] + Mod - X[j]) % Mod;
tmp = qpow(tmp, Mod - 2, Mod);
for(int j = 1; j <= N; j ++)
if(i != j) tmp = tmp * (K + Mod - X[j]) % Mod;
tmp = tmp * Y[i] % Mod, ans = (ans + tmp) % Mod;
}
printf("%lld", ans);
system("pause");
return 0;
}


# 自然幂数之和

CF622F The Sum of the k-th Powers

$S_k(n) = \sum_{i=1}^{n}i^k$

$$1\le n\le 10^9,\ 1\le k\le 10^6$$

## 性质

$$S_k(n)$$ 为关于 $$n$$$$k+1$$ 次多项式。

$$k=0$$ 时，$$S_k(n) = n$$，结论成立。
$$k=d$$ 时：

\begin{aligned} &(i+1)^{d+1} - i^{d+1}\\ = &\sum_{j=0}^{d+1}\left(\begin{matrix}d+1\\j\end{matrix}\right)i^j - i^{d+1}\\ = &\sum_{j=0}^{d}\left(\begin{matrix}d+1\\j\end{matrix}\right)i^j \end{aligned}

$$(i+1)^{d+1} - i^{d+1}$$ 求和，有：

\begin{aligned}&\sum_{i=1}^{n}\left\{(i+1)^{d+1} - i^{d+1}\right\}\\=&\sum_{i=1}^{n}\sum_{j=0}^{d}\left(\begin{matrix}d+1\\j\end{matrix}\right)i^j\end{aligned}

\begin{aligned}&\sum_{i=1}^{n}\sum_{j=0}^{d}\left(\begin{matrix}d+1\\j\end{matrix}\right)i^j\\=&\sum_{j=0}^{d}\left(\begin{matrix}d+1\\j\end{matrix}\right)\sum_{i=1}^{n}i^j\\=&\sum_{j=0}^{d}\left(\begin{matrix}d+1\\j\end{matrix}\right)S_j(n)\end{aligned}

$(n+1)^{d+1} - 1 = \sum_{j=0}^{d}\left(\begin{matrix}d+1\\j\end{matrix}\right)S_j(n)$

$\left(\begin{matrix}d+1\\d\end{matrix}\right)S_d(n) = (n+1)^{d+1} -\sum_{j=0}^{d-1}\left\{\left(\begin{matrix}d+1\\j\end{matrix}\right)S_j(n)\right\} - 1$

$S_d(n) = \dfrac{1}{d+1}\left\{\sum_{j=0}^{d+1}n^j -\sum_{j=0}^{d-1}\left\{\left(\begin{matrix}d+1\\j\end{matrix}\right)S_j(n)\right\} - 1\right\}$

## 具体实现

$S_k(n)= \sum_{i=1}^{k+2}S_k(i)\prod_{i\not ={j}}\dfrac{n-j}{i-j}$

$S_k(n)= \sum_{i=1}^{k+2}S_k(i)\dfrac{\prod\limits_{i\not ={j}}{(n-j)}}{\prod\limits_{i\not ={j}}(i-j)}$

\begin{aligned} &\dfrac{1}{\prod\limits_{i\not ={j}}(i-j)}\\ = &\dfrac{1}{i(i-1)(i-2) \dots 1 \times(-1) \dots (k+2-i-1)(k+2-i)}\\=&(-1)^{k+2-i}\dfrac{1}{i! (k+2-i)!} \end{aligned}

\begin{aligned} &\prod\limits_{i\not ={j}}{(n-j)}\\ =& n(n-1) \dots (n-(i-1))(n-(i+1))\dots (n-(k+2))\\ =&(\prod_{j=1}^{i-1}(n-j)) (\prod_{j=i+1}^{k+2}(n-j))& \end{aligned}

$S_k(n)= \sum_{i=1}^{k+2}(-1)^{k+2-i}S_k(i)\dfrac{(\prod\limits_{j=1}^{i-1}(n-j)) (\prod\limits_{j=i+1}^{k+2}(n-j))}{i! (k+2-i)!}$

## 复杂度

$$O(k \log k)$$ 处理 $$k+2$$ 个连续点值。
$$O(n)$$ 预处理前/后 缀积，阶乘。

# 写在最后

posted @ 2020-08-02 15:00  Luckyblock  阅读(336)  评论(0编辑  收藏  举报