Codeforces 438E The Child and Binary Tree

设答案的生成函数为 F(x)=ni=0f(i)xi , C(x)=mi=1xci 。那么

f(x)=i=1mjf(j)f(xcij)

这其实是一个卷积的形式,我们得到
F(x)=C(x)F2(x)+1

解得
F(x)=21±14C(x)

C(x)=0 ,可知应取加号。
然后就是多项式开根和求逆了。

#include
#include
using namespace std;
#define LL long long
const int maxn=1000000,p=998244353,g=3,inv2=499122177;
int a[maxn],b[maxn],c[maxn],x[maxn],y[maxn],
pw[maxn],rev[maxn],
n,m,inverse;
int rd()
{
    int x=0;
    char c=getchar();
    while (c<'0'||c>'9') c=getchar();
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x;
}
int inc(int x,int y)
{
    x+=y;
    return x>=p?x-p:x;
}
int dec(int x,int y)
{
    x-=y;
    return x<0?x+p:x;
}
int pow(int b,int k)
{
    int r=1;
    for (;k;k>>=1,b=(LL)b*b%p)
        if (k&1) r=(LL)r*b%p;
    return r;
}
void ntt(int *a,int l,int t,int flag)
{
    int x,t1,t2;
    for (int i=0;iif (rev[i]>i) swap(a[i],a[rev[i]]);
    for (int i=0;ifor (int j=0;j1<<(i+1))
        {
            x=0;
            for (int k=j;k1<1<*pw[x]%p;
                a[k]=inc(t1,t2);
                a[k+(1<x+=flag*(1<<(t-i-1));
                if (x<0) x+=l;
            }
        }
    if (flag==-1)
        for (int i=0;i*inverse%p;
}
void pre(int l,int t)
{
    inverse=pow(l,p-2);
    pw[0]=1;
    pw[1]=pow(g,(p-1)/l);
    for (int i=2;i1]*pw[1]%p;
    for (int i=0;i0;
        for (int j=0;j>j)&1)<<(t-j-1);
    }
}
void getinv(int *a,int *b,int l,int t)
{
    for (int i=0;ix[i]=a[i];
    for (int i=l>>1;i0;
    for (int i=l;i<(l<<1);i++) x[i]=b[i]=0;
    ntt(x,l<<1,t+1,1);
    ntt(b,l<<1,t+1,1);
    for (int i=0;i<(l<<1);i++)
        b[i]=(LL)b[i]*dec(2,(LL)x[i]*b[i]%p)%p;
    ntt(b,l<<1,t+1,-1);
}
void getrt(int *a,int *b,int *c,int l,int t)
{
    for (int i=0;i<(l>>1);i++) y[i]=c[i];
    for (int i=l>>1;i0;
    getinv(b,y,l,t);
    //y[0]=1;
    //for (int ll=2,tt=1;ll<=l;ll<<=1,tt++) getinv(b,y,ll,tt);
    for (int i=0;ix[i]=a[i];
    for (int i=l;i<(l<<1);i++) x[i]=y[i]=b[i]=0;
    ntt(x,l<<1,t+1,1);
    ntt(b,l<<1,t+1,1);
    ntt(y,l<<1,t+1,1);
    for (int i=0;i<(l<<1);i++)
        b[i]=(LL)inv2*inc(b[i],(LL)x[i]*y[i]%p)%p;
    ntt(b,l<<1,t+1,-1);
    getinv(b,c,l,t);
}
int main()
{
    //freopen("a.in","r",stdin);
    int x,L,T;
    n=rd();
    m=rd();
    for (int i=1;i<=n;i++)
    {
        x=rd();
        if (x<=m) a[x]=p-4;
    }
    a[0]=1;
    L=1,T=0;
    while (L<=m) L<<=1,T++;
    b[0]=c[0]=1;
    for (int l=2,t=1;l<=L;l<<=1,t++)
    {
        pre(l<<1,t+1);
        getrt(a,b,c,l,t);
    }
    b[0]=inc(b[0],1);
    for (int i=0;i0]=pow(a[0],p-2);
    for (int l=2,t=1;l<=L;l<<=1,t++)
    {
        pre(l<<1,t+1);
        getinv(a,b,l,t);
    }
    for (int i=1;i<=m;i++) printf("%d\n",inc(b[i],b[i]));
}

你可能感兴趣的:(数学,codeforces)