• 【洛谷P5158】【模板】多项式快速插值(分治NTT+拉格朗日插值)


    传送门


    考虑传统的拉格朗日插值法插多项式是O(n2)O(n^2)
    即构造函数f(x)=i=1nyij̸=i(xxj)xixjf(x)=sum_{i=1}^{n}y_iprod_{j ot= i}frac{(x-x_j)}{x_i-x_j}

    化一下

    f(x)=i=1nyij̸=i(xixj)j̸=i(xxj)f(x)=sum_{i=1}^{n}frac{y_i}{prod_{j ot= i}(x_i-x_j)}prod_{j ot= i}(x-x_j)

    考虑如何求出G=j̸=i(xixj)G=prod_{j ot= i}(x_i-x_j)
    g(x)=j(xxj)g(x)=prod_j(x-x_j)
    j̸=ij ot= i就相当于除以了xxix-x_i

    那就变成了g(xi)(xxi)frac{g(x_i)}{(x-x_i)}
    但是这个分子分母就都是0了,没法直接求

    根据洛必达法则:

    limxaf(x)=0,limxag(x)=0lim_{x ightarrow a}f(x)=0,lim_{x ightarrow a}g(x)=0


    limxaf(x)g(x)=limxaf(x)g(x)lim_{x ightarrow a}frac{f(x)}{g(x)}=lim_{x ightarrow a}frac{f'(x)}{g'(x)}

    同时取导得到G=g(xi)G=g'(x_i)
    接下来考虑对整个式子分治
    fl,rf_{l,r}表示分治[l,r][l,r]得到的答案

    fl,r=i=lryig(xi)j̸=i(xxj)f_{l,r}=sum_{i=l}^{r}frac{y_i}{g'(x_i)}prod_{j ot= i}(x-x_j)

    =k=mid+1r(xxk)i=lmidyig(xi)j̸=i[l,mid](xxj)+k=lmid(xxk)i=mid+1ryig(xi)j̸=i[mid+1,r](xxj)=prod_{k=mid+1}^{r}(x-x_k)sum_{i=l}^{mid}frac{y_i}{g'(x_i)}prod_{j ot= i}^{[l,mid]}(x-x_j)+prod_{k=l}^{mid}(x-x_k)sum_{i=mid+1}^{r}frac{y_i}{g'(x_i)}prod_{j ot =i}^{[mid+1,r]}(x-x_j)

    =i=mid+1r(xxi)fl,mid+i=lmid(xxi)fmid+1r=prod_{i=mid+1}^r(x-x_i)f_{l,mid}+prod_{i=l}^{mid}(x-x_i)f_{mid+1,r}

    先分治nttntt求出gg,多点求值把g(xi)g'(x_i)求出来再分治一波就完了

    复杂度O(nlog2n)O(nlog^2n)

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    inline char gc(){
    	static char ibuf[RLEN],*ib,*ob;
    	(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    	return (ob==ib)?EOF:*ib++;
    }
    #define gc getchar
    inline int read(){
    	char ch=gc();
    	int res=0,f=1;
    	while(!isdigit(ch))f^=ch=='-',ch=gc();
    	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    	return f?res:-res;
    }
    #define ll long long	
    #define re register
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define pb push_back
    #define cs const
    const int mod=998244353,G=3;
    inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
    inline void Add(int &a,int b){a=add(a,b);}
    inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
    inline void Dec(int &a,int b){a=dec(a,b);}
    inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
    inline void Mul(int &a,int b){a=mul(a,b);}
    inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
    inline void chemx(int &a,int b){a<b?a=b:0;}
    inline void chemn(int &a,int b){a>b?a=b:0;}
    cs int N=(1<<17)+1,C=20;
    #define poly vector<int>
    #define bg begin
    poly w[C+1];
    int rev[N<<2];
    inline void init_rev(int lim){
    	for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
    }
    inline void init_w(){
    	for(int i=1;i<=C;i++)w[i].resize((1<<(i-1)));
    	int wn=ksm(G,(mod-1)/(1<<C));
    	w[C][0]=1;
    	for(int i=1;i<(1<<(C-1));i++)
    	w[C][i]=mul(w[C][i-1],wn);
    	for(int i=C-1;i;i--)
    	for(int j=0;j<(1<<(i-1));j++)
    	w[i][j]=w[i+1][j<<1];
    }
    inline void ntt(poly &f,int lim,int kd){
    	for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
    	for(int mid=1,l=1;mid<lim;mid<<=1,l++)
    	for(int i=0,a0,a1;i<lim;i+=(mid<<1))
    		for(int j=0;j<mid;j++){
    			a0=f[i+j],a1=mul(f[i+j+mid],w[l][j]);
    			f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
    		}
    	if(kd==-1){
    		reverse(f.begin()+1,f.begin()+lim);
    		for(int i=0,inv=ksm(lim,mod-2);i<lim;i++)Mul(f[i],inv);
    	}
    }
    inline int F(cs poly a,int x){
    	int p=1,res=0;
    	for(int i=0;i<a.size();i++,Mul(p,x))Add(res,mul(a[i],p));
    	return res;
    }
    inline poly operator +(cs poly &a,cs poly &b){
    	poly c(max(a.size(),b.size()),0);
    	for(int i=0;i<c.size();i++)c[i]=add(a[i],b[i]);
    	return c;
    }
    inline poly operator -(cs poly &a,cs poly &b){
    	poly c(max(a.size(),b.size()),0);
    	for(int i=0;i<c.size();i++)c[i]=dec(a[i],b[i]);
    	return c;
    }
    inline poly operator *(poly a,poly b){
    	int deg=a.size()+b.size()-1,lim=1;
    	if(deg<=128){
    		poly c(deg,0);
    		for(int i=0;i<a.size();i++)
    		for(int j=0;j<b.size();j++)
    		Add(c[i+j],mul(a[i],b[j]));
    		return c;
    	}
    	while(lim<deg)lim<<=1;
    	init_rev(lim);
    	a.resize(lim),ntt(a,lim,1);
    	b.resize(lim),ntt(b,lim,1);
    	for(int i=0;i<lim;i++)Mul(a[i],b[i]);
    	ntt(a,lim,-1),a.resize(deg);
    	return a;
    }
    inline poly Inv(poly a,int deg){
    	poly b,c(1,ksm(a[0],mod-2));
    	for(int lim=4;lim<(deg<<2);lim<<=1){
    		b=a,b.resize(lim>>1);
    		init_rev(lim);
    		b.resize(lim),ntt(b,lim,1);
    		c.resize(lim),ntt(c,lim,1);
    		for(int i=0;i<lim;i++)Mul(c[i],dec(2,mul(b[i],c[i])));
    		ntt(c,lim,-1),c.resize(lim>>1);
    	}c.resize(deg);return c;
    }
    inline poly operator /(poly a,poly b){
    	int lim=1,deg=a.size()-b.size()+1;
    	reverse(a.bg(),a.end());
    	reverse(b.bg(),b.end());
    	while(lim<deg)lim<<=1;
    	b=Inv(b,lim),b.resize(deg);
    	a=a*b,a.resize(deg);
    	reverse(a.bg(),a.end());
    	return a;
    }
    inline poly operator %(poly a,poly b){
    	poly c=a-(a/b)*b;
    	c.resize(b.size()-1);
    	return c;
    }
    inline poly deriv(poly a){
    	for(int i=0;i<a.size()-1;i++)a[i]=mul(a[i+1],i+1);
    	a.pop_back();return a;
    }
    #define lc (u<<1)
    #define rc ((u<<1)|1)
    #define mid ((l+r)>>1)
    poly f[N<<2];
    int n,x[N],y[N],g[N];
    inline void build(int u,int l,int r){
    	if(l==r){f[u].pb(mod-x[l]),f[u].pb(1);return;}
    	build(lc,l,mid),build(rc,mid+1,r);
    	f[u]=f[lc]*f[rc];
    }
    inline void calc(int u,int l,int r,poly res){
    	if(l==r){
    		g[l]=mul(ksm(F(res,x[l]),mod-2),y[l]);
    		return;
    	}
    	calc(lc,l,mid,res%f[lc]),calc(rc,mid+1,r,res%f[rc]);
    }
    inline poly getans(int u,int l,int r){
    	if(l==r)return poly(1,g[l]);
    	poly ansl=getans(lc,l,mid),ansr=getans(rc,mid+1,r);
    	return ansl*f[rc]+ansr*f[lc];
    }
    int main(){
    	n=read();
    	init_w();
    	for(int i=1;i<=n;i++)x[i]=read(),y[i]=read();
    	build(1,1,n);
    	calc(1,1,n,deriv(f[1]));
    	poly ans=getans(1,1,n);
    	for(int i=0;i<n;i++)cout<<ans[i]<<" ";
    }
    
  • 相关阅读:
    angular2.0学习笔记4.npm常用指令记录及angular语法
    angular2.0学习笔记7.echarts 地图(type:'map')slice undifined 出错问题
    angular2.0学习笔记6.编程风格指南
    ionic3搭建笔记及编译成apk
    Ionic3--数据存储
    webpack入门笔记
    angular2.0学习笔记3.了解angular2.0项目结构
    angular2.0学习笔记2.创建hello world项目
    UVA 10954 Add All
    POJ 3069 Saruman's Army
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/12328705.html
Copyright © 2020-2023  润新知