[Polynomial algorithm] inverse polynomial Study Notes

Inverse polynomial


And multiplicative inverse integer defining like, for polynomial \ (A (X) B (X) =. 1 \) , called \ (A (x), B (x) \) mutually multiplicative inverse.

\ (A (x) \) Sufficient Condition multiplicative inverse is \ ([x ^ 0] A (x) \) exists multiplicative inverse.

Now think about how to use \ (O (n \ log n ) \) time to calculate \ (A (x) \) of the multiplicative inverse:

Consider the multiplication, has been determined before the current \ (n-\) inverse term, then: \ (A (X) B (X) \ equiv. 1 \ X ^ n-PMOD {} \)
\ [\ the begin Equation} {\ begin {split} A (x) B (x) -1 & \ equiv 0 \ pmod {x ^ n} \\ \ Big (A (x) B (x) -1 \ Big) ^ 2 & \ equiv0 \ pmod {x ^ {2n}} \\ A ( x) ^ 2B (x) ^ 2-2A (x) B (x) + 1 & \ equiv 0 \ pmod {x ^ {2n}} \\ 2A (x) B (x ) -A (x) ^ 2B ( x) ^ 2 & \ equiv1 \ pmod {x ^ {2n}} \\ A (x) B (x) \ Big (2-A (x) B (x) \ Big) & \ equiv1 \ pmod {x ^
{2n}} \\ \ end {split} \ end {equation} \] we will be able to find \ (\ mod {x ^ { } 2n} \) inverse in: \ (B (X) \ Big (2-A (X) B (X) \ Big) \) , repeating \ (O (\ log n) \) times to get the answer.

Time complexity: \ (T (n-) = T (\ FRAC N2) + O (n-\ log n-) = O (n-\ log n-) \)

Code:

Example: P4238 [template] inverse polynomial

//Luogu-O2
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define rint register int
typedef long long ll;

#define Getchar (p1==p2&&(p2=(p1=In)+fread(In,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char In[1<<20],*p1=In,*p2=In,Ch,Out[1<<20],*Outp=Out,St[25],*Tp=St;
inline int Getint(register int x=0)
{
    while(!isdigit(Ch=Getchar));
    for(;isdigit(Ch);Ch=Getchar)x=x*10+(Ch^48);
    return x;
}
inline void Putint(int x,char c)
{
    do *Tp++=x%10^48;while(x/=10);
    do *Outp++=*--Tp;while(Tp!=St);
    *Outp++=c;
}

const int p=998244353,g1=3,g2=(p+1)/3;

inline int Add(const int a,const int b){return (a+b)>=p?a+b-p:a+b;}
inline ll Pow(ll a,ll b,ll s=1){for(;b;b>>=1,a=a*a%p)if(b&1)s=s*a%p;return s;}

namespace Poly
{
    int r[1<<18];
    void Pre(const int n){for(rint i=1,l=log2(n);i<n;++i)r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));}

    void NTT(const int n,int *A,const int g)
    {
        for(rint i=1;i<n;++i)if(i<r[i])std::swap(A[i],A[r[i]]);
        for(rint i=2,h=1;i<=n;i<<=1,h<<=1)
            for(rint j=0,Rs=Pow(g,(p-1)/i);j<n;j+=i)
                for(rint k=0,Rt=1;k<h;++k,Rt=(ll)Rt*Rs%p)
                {
                    int Tmp=(ll)A[j+h+k]*Rt%p;
                    A[j+h+k]=Add(A[j+k],p-Tmp),A[j+k]=Add(A[j+k],Tmp);
                }
        if(g==g2)for(rint i=0,In=Pow(n,p-2);i<n;++i)A[i]=(ll)A[i]*In%p;
    }

    int C[1<<18];
    void Inv(int n,int *A,int *B)
    {
        if(n==1){B[0]=Pow(A[0],p-2);return;}
        Inv(n>>1,A,B),memcpy(C,A,n*sizeof(int));
        Pre(n<<1),NTT(n<<1,C,g1),NTT(n<<1,B,g1);
        for(rint i=0;i<(n<<1);++i)B[i]=B[i]*(2-(ll)C[i]*B[i]%p+p)%p;
        NTT(n<<1,B,g2),memset(B+n,0,n*sizeof(int));
    }
}

int n,m,F[1<<18],G[1<<18];

int main()
{
    n=Getint()-1;
    for(rint i=0;i<=n;++i)F[i]=Getint();
    for(m=n,n=1;n<=m;n<<=1);
    Poly::Inv(n,F,G);
    for(rint i=0;i<=m;++i)Putint(G[i],i==m?'\n':' ');
    return fwrite(Out,1,Outp-Out,stdout),0;
}

Guess you like

Origin www.cnblogs.com/LanrTabe/p/11359952.html