結果

問題 No.3575 Sum of Inversion of Trees
コンテスト
ユーザー wasab1
提出日時 2026-06-19 21:18:57
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,390 ms / 2,000 ms
コード長 2,770 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,839 ms
コンパイル使用メモリ 429,752 KB
実行使用メモリ 112,708 KB
最終ジャッジ日時 2026-06-19 21:19:07
合計ジャッジ時間 9,340 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 11
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

using ll = long long;
using mint = modint998244353;
using fps = vector<mint>;

#define endl '\n'
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)

fps mul(const fps &a, const fps &b, int n){
  if(a.empty() || b.empty()){
    return fps(n, mint(0));
  }

  fps c = convolution(a, b);

  if((int)c.size() > n){
    c.resize(n);
  }
  else{
    c.resize(n, mint(0));
  }

  return c;
}

fps inv_fps(const fps &f, int n){
  fps g(1);
  g[0] = f[0].inv();

  int m = 1;
  while(m < n){
    int nm = min(2 * m, n);

    fps fcut(nm, mint(0));
    rep(i,0,min((int)f.size(), nm)){
      fcut[i] = f[i];
    }

    fps t = mul(fcut, g, nm);

    rep(i,0,nm){
      t[i] = -t[i];
    }
    t[0] += mint(2);

    g = mul(g, t, nm);
    m = nm;
  }

  return g;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;

  int L = N + 1;

  vector<mint> fact(N + 1), ifact(N + 1);

  fact[0] = 1;
  rep(i,1,N+1){
    fact[i] = fact[i - 1] * i;
  }

  ifact[N] = fact[N].inv();
  rrep(i,1,N+1){
    ifact[i - 1] = ifact[i] * i;
  }

  /*
    P(x) = sum_{n>=1} n^{n-1} x^n/n!
    根付きラベル付き木の EGF
  */
  fps P(L, mint(0));
  rep(i,1,N+1){
    P[i] = mint(i).pow(i - 1) * ifact[i];
  }

  fps P2 = mul(P, P, L);
  fps P3 = mul(P2, P, L);
  fps P4 = mul(P2, P2, L);

  /*
    Q(x) =
    e^P * P(1+P)(12-6P-4P^2+7P^3-3P^4) / (12(1-P)^3)

    ただし e^P = P/x なので、

    Q(x) = A(x)/x

    A(x)=P^2(1+P)(12-6P-4P^2+7P^3-3P^4)/(12(1-P)^3)

    g(1) = (N-1)! [x^{N-1}] Q(x)
         = (N-1)! [x^N] A(x)
  */

  fps S(L, mint(0));
  S[0] = 12;

  rep(i,0,L){
    S[i] -= mint(6) * P[i];
    S[i] -= mint(4) * P2[i];
    S[i] += mint(7) * P3[i];
    S[i] -= mint(3) * P4[i];
  }

  fps one_plus_P = P;
  one_plus_P[0] += 1;

  fps A = mul(P2, one_plus_P, L);
  A = mul(A, S, L);

  fps one_minus_P(L, mint(0));
  one_minus_P[0] = 1;
  rep(i,0,L){
    one_minus_P[i] -= P[i];
  }

  fps inv1 = inv_fps(one_minus_P, L);
  fps inv2 = mul(inv1, inv1, L);
  fps inv3 = mul(inv2, inv1, L);

  A = mul(A, inv3, L);

  mint inv12 = mint(12).inv();
  rep(i,0,L){
    A[i] *= inv12;
  }

  mint g1 = fact[N - 1] * A[N];

  /*
    g(M) = g(1) - D(M-1)(N-M)

    D = (7N-12)N^{N-5}
    ただし小さい N は個別処理
  */
  mint D = 0;

  if(N <= 2){
    D = 0;
  }
  else if(N == 3){
    D = 1;
  }
  else if(N == 4){
    D = 4;
  }
  else{
    D = mint(7LL * N - 12) * mint(N).pow(N - 5);
  }

  rep(M,1,N+1){
    mint ans = g1 - D * mint(M - 1) * mint(N - M);
    cout << ans.val() << endl;
  }

  return 0;
}
0