luogu P4238 【模板】多项式乘法逆
题面传送门
我们考虑倍增,即算出\(G_0\)使得\(G_0*F\equiv1\pmod{x^{\frac{n+1}{2}}}\)
然后我们要求的就是\(G*F\equiv1\pmod{x^{\frac{n+1}{2}}}\)
所以\(G-G_0\equiv0\pmod{x^{\frac{n+1}{2}}}\)
两边平方,得到\((G-G_0)^2\equiv0\pmod{x^{n+1}}\)
即\((G-G_0)^2\equiv0\pmod{x^{n}}\)
展开得到\(G^2-2\times G_0G+G_0^2\equiv0\pmod{x^{n}}\)
两边同时除\(G\)就有\(2\times G_0-G_0^2F\equiv G\pmod{x^{n}}\)
然后直接\(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 300000
#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))
using namespace std;
int n,m,tr[N+5];ll A[N+5],B[N+5],C[N+5],D[N+5],invn;
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;}const ll G=3,invG=mpow(G);
I void swap(ll &x,ll &y){x^=y^=x^=y;}
I void NTT(ll *A,int n,int flag){
ll key,now,pus;re int i,j,h;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;
}
}
}
I void Getinv(ll *A,ll *B,int n){
if(n==1) return (void)(B[0]=mpow(A[0]));Getinv(A,B,n+1>>1);re int i;
for(m=1;m<=(n<<1);m<<=1);for(i=0;i<m;i++) tr[i]=(tr[i>>1]>>1)|((i&1)?(m>>1):0);for(i=0;i<n;i++) C[i]=A[i];for(i=n;i<m;i++) C[i]=0;NTT(C,m,1);NTT(B,m,1);
for(i=0;i<m;i++) B[i]=B[i]*(2-C[i]*B[i]%mod+mod)%mod;NTT(B,m,0);invn=mpow(m);for(i=0;i<n;i++)B[i]=B[i]*invn%mod;for(i=n;i<m;i++)B[i]=0;
}
int main(){
freopen("1.in","r",stdin);
re int i;scanf("%d",&n);for(i=0;i<n;i++) scanf("%lld",&A[i]);Getinv(A,B,n);for(i=0;i<n;i++) printf("%lld ",B[i]);
}