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==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,p[N],size[N],fac[N],inv[N],r[N*3],f[N*3],g[N*3],ans[N],t;
struct data{int to,nxt;
}edge[N<<1];
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=1;
    for (;k;k>>=1,a=1ll*a*a%P) if (k&1) s=1ll*s*a%P;
    return s;
}
int C(int n,int m){if (m>n) return 0;return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;}
void dfs(int k,int from)
{
    size[k]=1;
    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=1;k<=n;k++)
    {
        int ans=1ll*C(n,k)*(n+1)%P;
        for (int i=1;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=0;i<n;i++) if (i<r[i]) swap(a[i],a[r[i]]);
    for (int i=2;i<=n;i<<=1)
    {
        int wn=ksm(g,(P-1)/i);
        for (int j=0;j<n;j+=i)
        {
            int w=1;
            for (int k=j;k<j+(i>>1);k++,w=1ll*w*wn%P)
            {
                int x=a[k],y=1ll*w*a[k+(i>>1)]%P;
                a[k]=(x+y)%P;a[k+(i>>1)]=(x-y+P)%P;
            }
        }
    }
}
void work()
{
    memset(f,0,sizeof(f));memset(g,0,sizeof(g));
    int t=1;while (t<=(n<<1)) t<<=1;
    for (int i=0;i<t;i++) r[i]=(r[i>>1]>>1)|(i&1)*(t>>1);
    for (int i=1;i<=n;i++) f[size[i]]=(f[size[i]]+fac[size[i]])%P;
    reverse(f,f+n+1);
    for (int i=0;i<=n;i++) g[i]=inv[i];
    DFT(f,t,5),DFT(g,t,5);
    for (int i=0;i<t;i++) f[i]=1ll*f[i]*g[i]%P;
    DFT(f,t,ksm(5,P-2));
    reverse(f,f+n+1);
    int u=ksm(t,P-2);
    for (int i=1;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=1;i<n;i++)
    {
        int x=read(),y=read();
        addedge(x,y),addedge(y,x);
    }
    dfs(1,1);
    fac[0]=1;for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%P;
    inv[0]=inv[1]=1;for (int i=2;i<=n;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P;
    for (int i=2;i<=n;i++) inv[i]=1ll*inv[i-1]*inv[i]%P;
    //force();
    for (int i=1;i<=n;i++) ans[i]=1ll*C(n,i)*(n+1)%P;
    work();
    for (int i=1;i<=n;i++) size[i]=n-size[i];
    work();
    for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
    return 0;
}

 

posted @ 2019-01-14 18:49  Gloid  阅读(154)  评论(0编辑  收藏  举报