AGC005F Many Easy Problems(NTT)

时间:2023-03-09 14:42:01
AGC005F Many Easy Problems(NTT)

  先只考虑求某个f(k)。考虑转换为计算每条边的贡献,也即该边被所选连通块包含的方案数。再考虑转换为计算每条边不被包含的方案数。这仅当所选点都在该边的同一侧。于是可得f(k)=C(n,k)+ΣC(n,k)-C(sizei,k)-C(n-sizei,k)。于是就可以O(n)求出某个f(k)了。

  现在要求所有f(k),容易发现是一个卷积的形式,并且所给模数是一个隐蔽的NTT模数(最小原根是5),直接NTT即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 200010
#define P 924844033
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,p[N],size[N],fac[N],inv[N],r[N*],f[N*],g[N*],ans[N],t;
struct data{int to,nxt;
}edge[N<<];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
int ksm(int a,int k)
{
int s=;
for (;k;k>>=,a=1ll*a*a%P) if (k&) s=1ll*s*a%P;
return s;
}
int C(int n,int m){if (m>n) return ;return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;}
void dfs(int k,int from)
{
size[k]=;
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=from)
{
dfs(edge[i].to,k);
size[k]+=size[edge[i].to];
}
}
void force()
{
for (int k=;k<=n;k++)
{
int ans=1ll*C(n,k)*(n+)%P;
for (int i=;i<=n;i++)
ans=((ans-C(size[i],k)-C(n-size[i],k))%P+P)%P;
printf("%d\n",ans);
}
}
void DFT(int *a,int n,int g)
{
for (int i=;i<n;i++) if (i<r[i]) swap(a[i],a[r[i]]);
for (int i=;i<=n;i<<=)
{
int wn=ksm(g,(P-)/i);
for (int j=;j<n;j+=i)
{
int w=;
for (int k=j;k<j+(i>>);k++,w=1ll*w*wn%P)
{
int x=a[k],y=1ll*w*a[k+(i>>)]%P;
a[k]=(x+y)%P;a[k+(i>>)]=(x-y+P)%P;
}
}
}
}
void work()
{
memset(f,,sizeof(f));memset(g,,sizeof(g));
int t=;while (t<=(n<<)) t<<=;
for (int i=;i<t;i++) r[i]=(r[i>>]>>)|(i&)*(t>>);
for (int i=;i<=n;i++) f[size[i]]=(f[size[i]]+fac[size[i]])%P;
reverse(f,f+n+);
for (int i=;i<=n;i++) g[i]=inv[i];
DFT(f,t,),DFT(g,t,);
for (int i=;i<t;i++) f[i]=1ll*f[i]*g[i]%P;
DFT(f,t,ksm(,P-));
reverse(f,f+n+);
int u=ksm(t,P-);
for (int i=;i<=n;i++) ans[i]=(ans[i]-1ll*f[i]*u%P*inv[i]%P+P)%P;
}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
n=read();
for (int i=;i<n;i++)
{
int x=read(),y=read();
addedge(x,y),addedge(y,x);
}
dfs(,);
fac[]=;for (int i=;i<=n;i++) fac[i]=1ll*fac[i-]*i%P;
inv[]=inv[]=;for (int i=;i<=n;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P;
for (int i=;i<=n;i++) inv[i]=1ll*inv[i-]*inv[i]%P;
//force();
for (int i=;i<=n;i++) ans[i]=1ll*C(n,i)*(n+)%P;
work();
for (int i=;i<=n;i++) size[i]=n-size[i];
work();
for (int i=;i<=n;i++) printf("%d\n",ans[i]);
return ;
}