luogu P5667 拉格朗日插值2
题面传送门
我们考虑拉格朗日插值的式子\(\sum\limits_{i=0}^{n}{y_i\prod\limits_{j\ne i}{\frac{k-x_j}{x_i-x_j}}}\)
然后这道题式子是连续的,所以可以处理\(inv_n=\frac{1}{n!}\)
然后提前一些东西得到\(\frac{k!}{(k-n-1)!}\sum\limits_{i=0}^{n}{y_i\times inv_i\times inv_{n-i}\times \frac{1}{k-i}}\)
这不就可以NTT卷积了吗?时间复杂度\(O(nlogn)\)
code:
#include<bits/stdc++.h>
#define I inline
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define re register
#define ll long long
#define db double
#define N 600000
#define M 200000
#define mod 998244353
#define eps (1e-7)
#define U unsigned int
#define IT set<ques>::iterator
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
int n,m,k,tr[N+5];ll invn,ans=1,tot,pus,inv[N+5],F[N+5],H[N+5];
I ll mpow(ll x,int y=mod-2){ll ans=1;while(y) (y&1)&&(ans=ans*x%mod),y>>=1,x=x*x%mod;return ans;}
I void swap(ll &x,ll &y){x^=y^=x^=y;}const ll G=3,invG=mpow(G);
I void Make(int n,int &k){for(k=1;k<(n<<1);k<<=1);for(int i=0;i<k;i++) tr[i]=(tr[i>>1]>>1)|((i&1)?(k>>1):0);}
I void NTT(ll *A,int n,int flag){
re int i,j,h;ll now,pus,key;for(i=0;i<n;i++)i<tr[i]&&(swap(A[i],A[tr[i]]),0);
for(i=2;i<=n;i<<=1){
for(key=mpow(flag?G:invG,(mod-1)/i),j=0;j<n;j+=i){
for(now=1,h=j;h<j+i/2;h++)pus=now*A[h+i/2]%mod,A[h+i/2]=(A[h]-pus+mod)%mod,A[h]=(A[h]+pus)%mod,now=now*key%mod;
}
}if(flag) return;for(invn=mpow(n),i=0;i<n;i++)A[i]=A[i]*invn%mod;
}
int main(){
freopen("1.in","r",stdin);
re int i;scanf("%d%d",&n,&m);for(i=0;i<=n;i++) scanf("%lld",&F[i]);for(inv[0]=i=1;i<=n;i++) inv[i]=inv[i-1]*i%mod;inv[n]=mpow(inv[n]);for(i=n-1;~i;i--) inv[i]=inv[i+1]*(i+1)%mod;
for(i=0;i<=n;i++) F[i]=F[i]*inv[i]%mod*((n-i)&1? mod-inv[n-i]:inv[n-i])%mod;for(i=0;i<=2*n+1;i++) H[i]=mpow(m+i-n-1);Make(n*1.5,k);NTT(F,k,1);NTT(H,k,1);
for(i=0;i<k;i++) F[i]=F[i]*H[i]%mod;NTT(F,k,0);for(i=0;i<=n;i++) ans=ans*(m-i)%mod;for(i=n+1;i<=2*n+1;i++) printf("%lld\n",F[i]*ans%mod),ans=ans*(m+i-n)%mod*mpow(m+i-2*n-1)%mod;
}