QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#783247#9566. TopologyDarwin_ArnoldRE 0ms0kbC++202.1kb2024-11-26 03:00:432024-11-26 03:00:43

Judging History

你现在查看的是最新测评结果

  • [2024-11-26 03:00:43]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-26 03:00:43]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5010;
ll mod=998244353;
int n;
vector<int>G[N];
ll L[N];
int sz[N];
int p[N];
ll f[N];
ll g[N][N];
ll dp[N][N];
ll S[N][N];
ll ans[N];
ll fast_pow(ll a,ll m)
{
    ll base=a;
    ll res=1;
    while(m)
    {
        if(m&1ll)res=((res*1ll*base)%mod);
        base=(base*1ll*base)%mod;
        m>>=1;
    }
    return res;
}
ll mod_inverse(ll x)
{
    return (ll)(fast_pow(x,mod-2ll));
}
ll C(ll x,ll y)
{
    return (ll)((((L[x]*1ll*mod_inverse(L[y]))%mod)*1ll*mod_inverse(L[x-y]))%mod);
}
void dfs_1(int u,int fa)
{
    int temp=L[sz[u]-1];
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        dfs_1(v,u);
        temp=(ll)((temp*1ll*f[v])%mod);
        temp=(ll)((temp*1ll*mod_inverse(L[sz[v]]))%mod);
        //printf("%d\n",temp);
    }
    f[u]=temp;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        g[u][v]=(ll)((((f[u]*1ll*mod_inverse(f[v]))%mod)*mod_inverse(C(sz[u]-1,sz[v])))%mod);
    }
}
void dfs_2(int u,int fa)
{
    if(u!=1)
    {
        for(int i=1;i<=n;i++)
        {
            S[fa][i]=(ll)(((S[fa][i-1]*1ll+(dp[fa][i]*1ll*C(n-sz[u]-i,sz[fa]-sz[u]-1))%mod))%mod);
            dp[u][i]=(ll)(g[fa][u]*1ll*S[fa][i-1])%mod;
        }
    }
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        dfs_2(v,u);
    }
}
int main()
{
    scanf("%lld",&n);
    L[0]=1;
    for(int i=1;i<=n;i++)L[i]=(int)((i*1ll*L[i-1])%mod);
    for(int i=2;i<=n;i++)
    {
        int x;
        scanf("%lld",&x);
        p[i]=x;
        G[x].push_back(i);
    }
    for(int i=1;i<=n;i++)sz[i]=1;
    for(int i=n;i>=2;i--)sz[p[i]]+=sz[i];
    dp[1][1]=1;
    dfs_1(1,0);
    dfs_2(1,0);
    for(int i=1;i<=n;i++)
    {
        ans[i]=(ll)((((dp[i][i]*1ll*f[i])%mod)*1ll*C(n-i,sz[i]-1))%mod);
    }
    /*
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            printf("%d ",g[i][j]);
        }
        printf("\n");
    }
    */
    for(int i=1;i<=n;i++)
    {
        printf("%lld ",ans[i]);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

4
1 1 2

output:


result: