拉格朗日插值法

简陋的拉格朗日插值法学习过程

题目

已知 n 个点,确定了一个 n1 次多项式 f,求 f(x)

拉格朗日插值法

f(x)=i=1nyijixxjxixj

即可 O(n2) 计算

模板

直接套用公式即可

Code

#include <cstdio>
#define LL long long
using namespace std;
const int N = 2e3 + 5;
const LL P = 998244353;
int n, k, x[N], y[N];
inline LL fpow(LL x, LL y)
{
LL res = 1;
for(; y; y >>= 1, x = x * x % P) if (y & 1) res = res * x % P;
return res;
}
signed main()
{
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]);
LL ans = 0;
for(int i = 1; i <= n; i++)
{
LL s1 = y[i], s2 = 1;
for(int j = 1; j <= n; j++)
if (i != j) s1 = s1 * (k - x[j] + P) % P, s2 = s2 * (x[i] - x[j] + P) % P;
ans = (ans + s1 * fpow(s2, P - 2) % P) % P;
}
printf("%lld\n", ans);
}

自然数幂和

一个结论,和式是一个关于 nk+1 次多项式
把前 k+2 个点带进公式求 f(n) 即可
因为分母是阶乘形式的,分子分母都可以预处理
可以做到 O(nlogn)

Code

#include <cstdio>
#define re register
using namespace std;
typedef long long LL;
const int P = 1e9 + 7, N = 1e6 + 10;
int n, k;
LL pre[N], suf[N], fac[N];
inline int fpow(int x, int y)
{
x = (x + P) % P;
int res = 1;
for(; y; y >>= 1, x = 1LL * x * x % P) if (y & 1) res = 1LL * res * x % P;
return res;
}
int main()
{
scanf("%d%d", &n, &k);
pre[0] = 1, suf[k + 3] = 1, fac[0] = 1;
for(re int i = 1; i <= k + 2; i++) pre[i] = pre[i - 1] * (n - i + P) % P;
for(re int i = k + 2; i; i--) suf[i] = suf[i + 1] * (n - i + P) % P;
for(re int i = 1; i <= k + 2; i++) fac[i] = fac[i - 1] * i % P;
int ans = 0, x, y = 0;
for(re int i = 1; i <= k + 2; i++)
x = fpow(fac[i - 1] * ((k - i) & 1 ? -1 : 1) * fac[k + 2 - i] % P, P - 2),
y = (y + fpow(i, k)) % P, ans = (ans + pre[i - 1] * suf[i + 1] % P * x % P * y % P) % P;
printf("%d\n", ans);
}
posted @   leiyuanze  阅读(231)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示