P4781 拉格朗日插值
#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef long long ll; typedef pair<int,int> Pii; const ll mod=998244353; const int maxn = 2e3+100; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // head int x[maxn],y[maxn]; int main(){ int n,k; scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]); ll sum=0; for(int i=1;i<=n;i++){ ll fz=y[i],fm=1; for(int j=1;j<=n;j++){ if(j==i) continue; fz=(fz*(k-x[j])%mod+mod)%mod; fm=(fm*(x[i]-x[j])%mod+mod)%mod; } fm=powmod(fm,mod-2); sum=(sum+fz*fm%mod)%mod; } printf("%lld\n",sum); }