1. 程式人生 > >Luogu 4781 【模板】拉格朗日插值

Luogu 4781 【模板】拉格朗日插值

模板題。

拉格朗日插值的精髓在於這個公式

$$f(x) = \sum_{i = 1}^{n}y_i\prod _{j \neq i}\frac{x - x_i}{x_j - x_i}$$

其中$(x_i, y_i)$是給定的$n$個點值。

代入任何一個給定的點值座標$x_k$,都會發現這個式子等於$y_k$成立,因為對於任何$i \neq k$,後面的係數都至少有一項為$0$,而當$i == k$的時候,後面那一項一定為$1$,這樣子就可以保證代進去的點值一定被滿足。

因為題目中要求直接代入$x$求值,所以在算這個式子的時候直接把$x$代進去計算就可以了,時間複雜度$O(n^2)$,要不然求係數的過程相當於高斯消元,時間複雜度$O(n^3)$。

Code:

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;

const int N = 2005;
const ll P = 998244353LL;

int n;
ll k, xi[N], yi[N];

template <typename T>
inline void read(T &X) {
    X = 0; char ch = 0; T op = 1;
    for (; ch > '
9' || ch < '0'; ch = getchar()) if (ch == '-') op = -1; for (; ch >= '0' && ch <= '9'; ch = getchar()) X = (X << 3) + (X << 1) + ch - 48; X *= op; } inline ll fpow(ll x, ll y) { ll res = 1LL; for (; y > 0; y >>= 1) {
if (y & 1) res = res * x % P; x = x * x % P; } return res; } int main() { read(n), read(k); for (int i = 1; i <= n; i++) read(xi[i]), read(yi[i]); ll ans = 0LL; for (int i = 1; i <= n; i++) { ll mul1 = 1LL, mul2 = 1LL; for (int j = 1; j <= n; j++) { if (i == j) continue; mul1 = mul1 * ((k - xi[j] + P) % P) % P; mul2 = mul2 * ((xi[i] - xi[j] + P) % P) % P; } ans = (ans + yi[i] * mul1 % P * fpow(mul2, P - 2) % P) % P; } printf("%lld\n", ans); return 0; }
View Code