Luo Gu [4238] [template] inverse polynomial multiplication

Click here to see the problem surface

Generally meaning of the questions: given polynomial \ (F. (X) \) , seeking \ (G (x) \) satisfies \ (F. (X) * G (X) \ equiv1 (MOD \ X ^ n-) \) , the \ (998,244,353 \) modulo.

Foreword

Board problem will only copy solution to a problem ......

I have to say, though hard to think of this idea, but watching the solution to a problem or well understood.

Push formula

We can recursively to solve this problem.

The boundary, apparently when the recursive \ (n = 0 \) when, can be obtained directly \ (G (x) \) in \ (0 \) coefficient term views (i.e. constant term) is \ (F (x ) \) in \ (0 \) inverse order term coefficient.

Otherwise, it assumes that we currently known:

\[F(x)*H(x)\equiv1(mod\ x^{\lfloor\frac n2\rfloor})\]

Because of \ (F. (X) * G (X) \ equiv1 (MOD \ ^ n-X) \) , it is apparent

\[F(x)*G(x)\equiv1(mod\ x^{\lfloor\frac n2\rfloor})\]

Then, if we subtract the two equations, you can get:

\[F(x)*(G(x)-H(x))\equiv0(mod\ x^{\lfloor\frac n2\rfloor})\]

The \ (F (x) \) removed, get:

\[G(x)-H(x)\equiv0(mod\ x^{\lfloor\frac n2\rfloor})\]

We now consider the requirements of a polynomial, meet with \ (F (x) \) convolution module \ (x ^ n \) I \ (1 \) .

It is clear that we must find a way, let modulus here by \ (x ^ {\ lfloor \ frac n2 \ rfloor} \) becomes \ (the n-the X-^ \) .

How to do it? The square can simultaneously on both sides of the equation. which is:

\[(G(x)-H(x))^2\equiv0(mod\ x^n)\]

Demolition of the square with a perfect square formula:

\[G(x)^2-2G(x)*H(x)+H(x)^2\equiv0(mod\ x^n)\]

Then, when we then \ (F (x) \) convolution up, we get:

\[F(x)*G(x)^2-2F(x)*G(x)*H(x)+F(x)*H(x)^2\equiv0(mod\ x^n)\]

Since \ (F (the X-) * G (the X-) \ equiv1 (MOD \ the n-the X-^) \) , so we can get further simplified:

\[G(x)-2H(x)+F(x)*H(x)^2\equiv0(mod\ x^n)\]

At this point we can find formulas \ (G (x) \) have been independent, so long as you can get through the shift key:

\[G(x)\equiv2H(x)-F(x)*H(x)^2(mod\ x^n)\]

On this formula, as long as \ (NTT \) do polynomial multiplication on it.

Note that in a specific implementation, \ (G (X) \) and \ (H (x) \) can present an array.

Code

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 100000
#define X 998244353
#define Qinv(x) Qpow(x,X-2)
#define swap(x,y) (x^=y^=x^=y)
using namespace std;
int n,a[N+5],b[4*N+5];
class FastIO
{
    private:
        #define FS 100000
        #define tc() (A==B&&(B=(A=FI)+fread(FI,1,FS,stdin),A==B)?EOF:*A++)
        #define pc(c) (C==E&&(clear(),0),*C++=c)
        #define tn (x<<3)+(x<<1)
        #define D isdigit(c=tc())
        int T;char c,*A,*B,*C,*E,FI[FS],FO[FS],S[FS];
    public:
        I FastIO() {A=B=FI,C=FO,E=FO+FS;}
        Tp I void read(Ty& x) {x=0;W(!D);W(x=tn+(c&15),D);}
        Tp I void write(Ty x) {W(S[++T]=x%10+48,x/=10);W(T) pc(S[T--]);}
        Tp I void write(Con Ty& x,Con char& y) {write(x),pc(y);}
        I void clear() {fwrite(FO,1,C-FO,stdout),C=FO;}
}F;
I int Qpow(RI x,RI y) {RI t=1;W(y) y&1&&(t=1LL*t*x%X),x=1LL*x*x%X,y>>=1;return t;}
template<int SZ,int PR> class Poly
{
    private:
        int IPR,P,L,R[4*N+5],t[4*N+5];
        I void T(int *s,CI op)
        {
            RI i,j,k,U,S,x,y;for(i=0;i^P;++i) i<R[i]&&swap(s[i],s[R[i]]);
            for(i=1;i^P;i<<=1) for(U=Qpow(~op?PR:IPR,(X-1)/(i<<1)),j=0;j^P;j+=i<<1)
                for(S=1,k=0;k^i;++k,S=1LL*S*U%X) s[j+k]=((x=s[j+k])+(y=1LL*S*s[i+j+k]%X))%X,s[i+j+k]=(x-y+X)%X;
        }
    public:
        I Poly() {IPR=Qinv(PR);}
        I void Inv(CI n,int *a,int *b)
        {
            if(!n) return (void)(b[0]=Qinv(a[0]));Inv(n>>1,a,b);//处理边界,不在边界则继续递归
            RI i;P=1,L=0;W(P<=(n<<1)) P<<=1,++L;for(i=0;i^P;++i) R[i]=(R[i>>1]>>1)|((i&1)<<L-1);//预处理
            for(i=0;i<=n;++i) t[i]=a[i];T(b,1),T(t,1);//NTT
            for(i=0;i^P;++i) b[i]=(2LL*b[i]%X-1LL*t[i]*b[i]%X*b[i]%X+X)%X;T(b,-1);//计算新多项式
            RI t=Qinv(P);for(i=0;i<=n;++i) b[i]=1LL*b[i]*t%X;for(i=n+1;i^P;++i) b[i]=0;//注意将高位清空,否则会影响后续操作
        }
};Poly<N,3> P;
int main()
{
    RI i;for(F.read(n),--n,i=0;i<=n;++i) F.read(a[i]);//读入
    for(P.Inv(n,a,b),i=0;i<=n;++i) F.write(b[i]," \n"[i==n]);return F.clear(),0;//输出
}

Guess you like

Origin www.cnblogs.com/chenxiaoran666/p/Luogu4238.html