codeforces E. The Child and Binary Tree (inverse polynomial, polynomial root opening, DP)

Questions surface: https://codeforces.com/problemset/problem/438/E

Solution: set \ [F (n) \] is the weight and the number n have the binary number \ [G (n) \] represents the set value, there is no right to the number n

Then: when n = 0,, \ [f (n) = 1 \]

When n! = 0, the root node weights first enumerated, and the enumeration of the number of left and right subtrees

\[f(n) = \sum\limits_{i = 0}^n {g(i)\sum\limits_{j = 0}^{n - i} {f(j){\rm{\cdot}}f(n - i - j)} } \]

令\[F(x) = \sum\limits_{i = 0}^\infty  {f(i){\rm{\cdot}}{x^i}} \],\[G(x) = \sum\limits_{i = 0}^\infty  {g(i){\rm{\cdot}}{x^i}} \]

Then \ [F = G {\ {*} rm} {F ^ 2} {\ rm {+}} 1 \] solve the equation

Solvable: \ [\ frac {{1 {\ rm {\ pm}} \ sqrt {1 - 4 {\ rm {G}}}}} {{2G}} \]

Solutions for rounding positive number, the answer is \ [\ frac {{1 - \ sqrt {1 - 4 {\ rm {G}}}}} {{2G}} \] coefficient, the next step is the inverse polynomial and square root

#include<bits/stdc++.h>
#define ms(x) memset(x,0,sizeof(x))
#define sws ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int maxn=4e5+5;
const double pi=acos(-1.0);
const ll mod=998244353;///通常情况下的模数,
const ll g=3;///模数的原根998244353,1004535809,469762049
ll qpow(ll a,ll n,ll p){
    ll ans=1;
    while(n){
        if(n&1) ans=ans*a%p;
        n>>=1;
        a=a*a%p;
    }
    return ans;
}
int rev[maxn];
int inv2;
void ntt(int a[],int n,int len,int pd){
    rev[0]=0;
    for(int i=1;i<n;i++){
        rev[i]=(rev[i>>1]>>1| ((I & . 1 ) << (len . 1 )));
         IF (I < Rev [I]) the swap (A [I], A [Rev [I]]); 
    } 
    for ( int MID = . 1 ; MID <n-; MID = << . 1 ) { 
        LL Wn of = qpow (G, (mod- . 1 ) / (MID * 2 ), MOD); /// primitive root root unit instead of 
        IF (PD == - . 1 ) = Wn of qpow (Wn of, mod- 2 , MOD); /// inverse transform into the inverse element 
        for ( int J = 0 ; J <n-; = J + 2 * MID) { 
            LL W = . 1 ;
            for(int k=0;k<mid;k++){
                ll x=a[j+k],y=w*a[j+k+mid]%mod;
                a[j+k]=(x+y)%mod;
                a[j+k+mid]=(x-y+mod)%mod;
                w=w*wn%mod;
            }
        }
    }
    if(pd==-1){
        ll inv=qpow(n,mod-2,mod);
        for(int i=0;i<n;i++){
            a[i]=a[i]*inv%mod;

        }
    }
}
int A[maxn],B[maxn];
void solve(int *a,int *b,int n){
    int len=0,up=1;
    while(up<n) up<<=1,len++;
    ntt(a,up,len,1);
    ntt(b,up,len,1);
    for(int i=0;i<up;i++) a[i]=1ll*a[i]*b[i]%mod*b[i]%mod;
    ntt(a,up,len,-1);
}
void Inv(int *a,int *b,ll n){
    if(n==1){
        b[0]=qpow(a[0],mod-2,mod);
        return;
    }
    Inv(a,b,n>>1);
    for(int i=0;i<n;i++) A[i]=a[i],B[i]=b[i];
    solve(A,B,n<<1);
    for(int i=0;i<n;i++) b[i]=(2ll*b[i]%mod-A[i]+mod)%mod;
    for(int i=0;i<=2*n;i++) A[i]=B[i]=0;
}
int x[maxn],y[maxn],C[maxn],D[maxn],in[maxn];
void mul(int *a,int *b,int n){
      int len=0,up=1;
    while(up<n) up<<=1,len++;
    ntt(a,up,len,1);
    ntt(b,up,len,1);
    for(int i=0;i<up;i++) a[i]=1ll*a[i]*b[i]%mod;
    ntt(a,up,len,-1);
}
void Sqrt(int *a,int *b,ll n){
    if(n==1){
        b[0]=a[0];
        return;
    }
    Sqrt(a,b,n>>1);
    for(int i=0;i<n;i++) C[i]=a[i];
    Inv(b,D,n);
    mul(D,C,n<<1);
    for(int i=0;i<n;i++)b[i]=1ll*(b[i]+D[i])%mod*inv2%mod;
    for(int i=0;i<=n*2;i++) C[i]=D[i]=0;
    
}
int main(){
    int n,m;
    inv2=qpow(2,mod-2,mod);
    sws;
    cin>>n>>m;
    int up=0;
    for(int i=0;i<n;i++) {
        int c;
        cin>>c;
        up=max(c,up);
        x[c]=(-4+mod)%mod;
    }
    up=m+1;
    x[0]=(1-x[0]+mod)%mod;
    int len=1;
    while(len<up) len<<=1;
    Sqrt(x,y,len);
    y[0]++;
    Inv(y,in,len);
    for(int i=1;i<=m;i++) cout<<2*in[i]%mod<<endl; 
}

 

Guess you like

Origin www.cnblogs.com/azznaz/p/11531053.html