拉格朗日插值法
假设 n=4,
内容来自百度百科 ↑
所以,这是n个不同的点确定一条曲线??
大佬博客里面的链接里有讲到一些应用,值得看看
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
const int mod = 998244353;
int n, m, k;
int x[N], y[N];
ll qpow(ll a, ll b) {
a %= mod;
ll res = 1;
while (b) {
if (b & 1)res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> x[i] >> y[i];
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
ll up = y[i] % mod, down = 1;
for (int j = 1; j <= n; ++j) {
if (i == j) continue;
up = up * (k - x[j] + mod) % mod;
down = down * (x[i] - x[j] + mod) % mod;
}
ans = (ans + up * qpow(down, mod - 2) % mod) % mod;
}
cout << ans << endl;
return 0;
}