[洛谷P4781]【模板】拉格朗日插值
题目大意:给你$n(n\leqslant2000)$个点,要你求$n-1$次经过这$n$个点的多项式在$k$处的值
题解:$Lagrange$插值:
$$
f_x=\sum\limits_{i=1}^ky_i\prod\limits_{j=1,j\not=i}^k\dfrac{x-x_j}{x_i-x_j}
$$
卡点:无
C++ Code:
#include <algorithm> #include <cstdio> #define maxn 2010 const int mod = 998244353; namespace Math { inline int pw(int base, int p) { static int res; for (res = 1; p; p >>= 1, base = static_cast<long long> (base) * base % mod) if (p & 1) res = static_cast<long long> (res) * base % mod; return res; } inline int inv(int x) { return pw(x, mod - 2); } } inline void reduce(int &x) { x += x >> 31 & mod; } inline int getreduce(int x) { return x + (x >> 31 & mod); } int n, k, ans; int x[maxn], y[maxn]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d%d", x + i, y + i); for (int i = 1; i <= n; ++i) { long long a = y[i], b = 1; for (int j = 1; j <= n; ++j) if (i != j) { a = a * getreduce(k - x[j]) % mod; b = b * getreduce(x[i] - x[j]) % mod; } reduce(ans += a * Math::inv(b) % mod - mod); } printf("%d\n", ans); return 0; }