Luo Gu 4781: Lagrange
We know that \ (n + 1 \) points can determine a \ (n \) order polynomial.
How this polynomial seek it? This is the Lagrange polynomial, Lagrange interpolation polynomial can find.
For example a first direct, for example, a quadratic polynomial, of course, need three points.
For example, \ (F (. 3) = 2, F (. 4) =. 3, F (. 6) =. 1 \) .
First, write three polynomial:
- \(l_0(x)=\frac{(x-4)(x-6)}{(3-4)(3-6)}\).
- \(l_1(x)=\frac{(x-3)(x-6)}{(4-3)(4-6)}\).
- \(l_2(x)=\frac{(x-3)(x-4)}{(6-3)(6-4)}\).
\(P(x)=f(3)l_0(x)+f(4)l_1(x)+f(6)l_2(x)\).
\(=2\frac{(x-4)(x-6)}{(3-4)(3-6)}+3\frac{(x-3)(x-6)}{(4-3)(4-6)}+1\frac{(x-3)(x-4)}{(6-3)(6-4)}\).
\(=-\frac{2}{3}x^2+\frac{34}{6}x-9\).
You can be substituted into the verify:
- \(f(3)=-6+17-9=2\).
- \(f(4)=3\).
- \(f(6)=-24+34-9=1\).
This is the Lagrange polynomial.
Formula summarized as follows:
- \(F(x)=\sum_{i=1}^ny_i*\prod_{j\neq i}\frac{x-x_j}{x_i-x_j}\)。
This allows the (O (n ^ 2) \ ) \ determined interpolation polynomial time.
Of course, this problem requires that \ (f (k) \) values, so the \ (X \) band into \ (K \) on the line.
- \(ans=\sum_{i=1}^ny_i*\prod_{j\neq i}\frac{k-x_j}{x_i-x_j}\).
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2000 + 10; const int mod = 998244353; ll n, k, x[maxn], y[maxn]; ll qmi(ll a, ll b) { ll res = 1; while(b) { if(b&1) res = (res*a)%mod; a = (a*a)%mod; b >>= 1; } return res%mod; } ll ans, s1, s2; void Largrange() { for(int i = 1; i <= n; i++) { s1 = y[i] % mod; s2 = 1ll; for(int j = 1; j <= n; j++) { if(i != j) { s1 = s1*(k-x[j])%mod; s2 = s2*((x[i] - x[j]%mod)%mod)%mod; } } ans += s1 * qmi(s2, mod-2)%mod; ans = (ans+mod)%mod; } printf("%lld\n", ans); } int main() { scanf("%lld%lld", &n, &k); for(int i = 1; i <= n; i++) scanf("%lld%lld", &x[i], &y[i]); Largrange(); return 0; }