• P4781 【模板】拉格朗日插值


    \(\text{Note}\)

    考虑构造函数\(f\)

    \[f(k)=\sum_{i=1}^{n} y_i\prod_{j\neq i }\frac{k-x_j}{x_i-x_j} \]

    求多项式在某一位置的取值

    对于求\(f(k)\),将\(k\)带入即可,时间复杂度为\(O(n^2)\)

    求多项式每一项的系数

    考虑将\(f\)展开,时间复杂度的瓶颈在于求\(\prod_{j\neq i }(x-x_j)\)
    先预处理出\(S = \prod (x-x_j)\)
    所以\(\prod_{j\neq i }(x-x_j) = \frac{S}{x - x_i}\),此时用朴素的多项式除法即可,时间复杂度仍为\(O(n^2)\)

    \(\text{Code}\)

    求多项式在某一位置的取值

    #include<cstdio>
    #define LL long long
    using namespace std;
    const int P = 998244353;
    int n; LL m,x[2005],y[2005];
    
    LL fpow(int x,LL y)
    {
    	LL res = 1;
    	for (; x; x >>= 1,y = y * y % P)
    		if (x & 1) res = res * y % P;
    	return res;
    }
    int main()
    {
    	scanf("%d%lld",&n,&m);
    	for (int i = 1; i <= n; i++) scanf("%lld%lld",&x[i],&y[i]);
    	LL ans = 0;
    	for (int i = 1; i <= n; i++)
    	{
    		LL p = y[i],q = 1;
    		for (int j = 1; j <= n; j++) 
    			if (j != i) p = p * (m - x[j]) % P,q = q * (x[i] - x[j]) % P;
    		p = (p + P) % P,q = (q + P) % P;
    		ans = (ans + p * fpow(P - 2,q) % P) % P;
    	}
    	printf("%lld\n",ans);
     } 
    
  • 相关阅读:
    idea自定义servlet模板
    jsp基础-指令,内置对象,动作,EL表达式,JSTL技术
    cookie和session
    HttpServletRequest
    IO字符流
    IO字节流
    递归
    File 类
    JDBC数据库连接
    Map接口
  • 原文地址:https://www.cnblogs.com/nibabadeboke/p/15995106.html
Copyright © 2020-2023  润新知