# bzoj 3625

$f(n)=\sum_{i=0}^{n}g(i)\sum_{j=0}^{n-i}f(j)f(n-i-j)$

$G(x)F(x)^{2}-F(x)+1=0$

$F(x)=\frac{1+ \sqrt{1-4G(x)}}{2G(x)}$或$F(x)=\frac{1-\sqrt{1-4G(x)}}{2G(x)}$

$F(x)=\frac{1-[1-4G(x)]}{2G(x)[1-\sqrt{1-4G(x)}]}$或$F(x)=\frac{1-[1-4G(x)]}{2G(x)[1+\sqrt{1-4G(x)}]}$

$F(x)=\frac{2}{1-\sqrt{1-4G(x)}}$或$F(x)=\frac{2}{1+\sqrt{1-4G(x)}}$

bzoj严重卡常，我用了unsigned int

#include <cstdio>
#include <algorithm>
#define uint unsigned int
#define ll long long
using namespace std;
const uint mode=998244353;
uint inv2=mode-mode/2;
uint to[(1<<20)+5];
uint ig[(1<<20)+5],GF[(1<<20)+5];
uint G[(1<<20)+5],sG[(1<<20)+5],IG[(1<<20)+5],F[(1<<20)+5];
int n,m;
inline uint MOD(uint x,uint y)
{
return x+y>=mode?x+y-mode:x+y;
}
inline uint pow_mul(uint x,uint y)
{
uint ret=1;
while(y)
{
if(y&1)ret=(ll)(1ll*ret*x%mode);
x=(ll)(1ll*x*x%mode),y>>=1;
}
return ret;
}
inline void NTT(uint *a,int len,int k)
{
for(register int i=0;i<len;++i)if(i<to[i])swap(a[i],a[to[i]]);
for(register int i=1;i<len;i<<=1)
{
uint w0=pow_mul(3,(mode-1)/(i<<1));
for(register int j=0;j<len;j+=(i<<1))
{
uint w=1;
for(register int o=0;o<i;++o,w=(uint)(1ll*w*w0%mode))
{
uint w1=a[j+o],w2=(uint)(1ll*a[j+o+i]*w%mode);
a[j+o]=(w1+w2)%mode,a[j+o+i]=(w1-w2+mode)%mode;
}
}
}
if(k==-1)
{
uint inv=pow_mul(len,mode-2);
for(int i=1;i<(len>>1);i++)swap(a[i],a[len-i]);
for(int i=0;i<len;i++)a[i]=(uint)(1ll*a[i]*inv%mode);
}
}
uint A[(1<<20)+5],B[(1<<20)+5],C[(1<<20)+5];
inline void mul(uint *f,uint *g,uint len)
{
int lim=1,l=0;
while(lim<=2*len)lim<<=1,++l;
for(register int i=0;i<lim;++i)to[i]=((to[i>>1]>>1)|((i&1)<<(l-1))),A[i]=B[i]=0;
for(register int i=0;i<len;++i)A[i]=f[i],B[i]=g[i];
NTT(A,lim,1),NTT(B,lim,1);
for(register int i=0;i<lim;++i)C[i]=(uint)(1ll*A[i]*B[i]%mode);
NTT(C,lim,-1);
}
void get_inv(uint *f,uint *g,uint dep)
{
if(dep==1)
{
g[0]=pow_mul(f[0],mode-2);
return;
}
int nxt=(dep+1)>>1;
get_inv(f,g,nxt);
int lim=1,l=0;
while(lim<=2*dep)lim<<=1,l++;
for(register int i=0;i<lim;++i)to[i]=((to[i>>1]>>1)|((i&1)<<(l-1))),A[i]=B[i]=0;
for(register int i=0;i<dep;++i)A[i]=f[i],B[i]=g[i];
NTT(A,lim,1),NTT(B,lim,1);
for(register int i=0;i<lim;++i)C[i]=(uint)(1ll*A[i]*B[i]%mode*B[i]%mode);
NTT(C,lim,-1);
for(register int i=0;i<dep;++i)g[i]=(2*g[i]+mode-C[i])%mode;
}
void get_sqr(uint *f,uint *g,int dep)
{
if(dep==1)
{
g[0]=1;
return;
}
int nxt=(dep+1)>>1;
get_sqr(f,g,nxt);
for(register int i=0;i<dep;++i)ig[i]=0;
get_inv(g,ig,dep);
mul(g,g,dep);
for(register int i=0;i<dep;++i)GF[i]=MOD(C[i],f[i]),ig[i]=(int)(1ll*ig[i]*inv2%mode);
mul(GF,ig,dep);
for(register int i=0;i<dep;++i)g[i]=C[i];
}
{
int f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
for(register int i=1;i<=n;++i)
{
G[x]++;
}
for(register int i=1;i<=m;++i)G[i]=(uint)(mode-G[i]*4ll%mode);
G[0]=1;
m++;
get_sqr(G,sG,m);
sG[0]++;
get_inv(sG,IG,m);
for(register int i=1;i<m;++i)F[i]=(uint)(2ll*IG[i]%mode),printf("%u\n",F[i]);
return 0;
}

posted @ 2019-06-19 15:24  lleozhang  Views(...)  Comments(...Edit  收藏
levels of contents