备战NOIP——模板复习16
这里只有模板,并不作讲解,仅为路过的各位做一个参考以及用做自己复习的资料,转载注明出处。
拉格朗日插值
/*Copyright: Copyright (c) 2018
*Created on 2018-11-04
*Author: 十甫
*Version 1.0
*Title: 拉格朗日插值
*Time: inf mins
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const int size = 2005;
inline ll Mod(ll t) {
return (t + mod) % mod;
}
ll n, k, x[size], y[size], ans, s1, s2;
inline ll mul(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1) res = Mod(Mod(res) * Mod(a));
a = Mod(Mod(a) * Mod(a));
b /= 2;
}
return res;
}
inline ll inv(ll t) {
return mul(t, mod - 2);
}
int main() {
scanf("%lld%lld", &n, &k);
for(int i = 1;i <= n;i++) {
scanf("%lld%lld", &x[i], &y[i]);
}
for(int i = 1;i <= n;i++) {
s1 = Mod(y[i]), s2 = 1ll;
for(int j = 1;j <= n;j++) if(i != j) {
s1 = Mod(Mod(s1) * Mod(Mod(k) - Mod(x[j])));
s2 = Mod(Mod(s2) * Mod(Mod(x[i]) - Mod(x[j])));
}
ans = Mod(Mod(ans) + Mod(Mod(s1) * inv(s2)));
}
printf("%lld\n", ans);
return 0;
}
NOIP 2018 RP++