P4781 【模板】拉格朗日插值

\(\color{#0066ff}{ 题目描述 }\)

小学知识可知,\(n\)个点(\(x_i,y_i\))可以唯一地确定一个多项式

现在,给定\(n\)个点,请你确定这个多项式,并将\(k\)代入求值

求出的值对\(998244353\)取模

\(\color{#0066ff}{输入格式}\)

第一行两个正整数\(n,k\),含义如题

接下来\(n\)行,每行两个正整数\(x_i,y_i\),含义如题

\(\color{#0066ff}{输出格式}\)

一个整数表示答案

\(\color{#0066ff}{输入样例}\)

3 100
1 4
2 9
3 16
    
3 100
1 1
2 2
3 3

\(\color{#0066ff}{输出样例}\)

10201
    
100

\(\color{#0066ff}{数据范围与提示}\)

n≤2000xi,yi,k≤998244353

样例一中的三个点确定的多项式是\(f(x)=x^2+2x+1\),将\(100\)代入求值得到\(10201\)

样例二中的三个点确定的多项式是\(f(x)=x\),将\(100\)代入求值得到\(100\)

如果你不会拉格朗日插值,你可以到这里去学习一下

此外,请注意算法的常数问题,建议开启O2优化

\(\color{#0066ff}{题解}\)

上面的“这里“(大雾

#include<bits/stdc++.h>
#define LL long long
LL in() {
	char ch; LL x = 0, f = 1;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
	return x * f;
}
const int maxn = 2050;
const int mod = 998244353;
int n;
LL x[maxn], y[maxn], k, ans;
LL ksm(LL x, LL y) {
	LL re = 1LL;
	while(y) {
		if(y & 1) re = re * x % mod;
		x = x * x % mod;
		y >>= 1;
	}
	return re;
}
int main() {
	n = in(), k = in();
	for(int i = 1; i <= n; i++) x[i] = in(), y[i] = in();
	for(int i = 1; i <= n; i++) {
		LL tot = 1;
		for(int j = 1; j <= n; j++) {
			if(j == i) continue;
			(tot *= ((k - x[j]) % mod + mod) % mod * ksm(((x[i] - x[j]) % mod + mod) % mod, mod - 2) % mod) %= mod;
		}
		(ans += tot * y[i] % mod) %= mod;
	}
	printf("%lld", ans);
	return 0;
}
posted @ 2019-02-23 18:20  olinr  阅读(139)  评论(0编辑  收藏  举报