結果

問題 No.1614 Majority Painting on Tree
ユーザー risujiroh
提出日時 2021-07-21 22:43:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 424 ms / 5,000 ms
コード長 1,498 bytes
コンパイル時間 1,876 ms
コンパイル使用メモリ 203,184 KB
最終ジャッジ日時 2025-01-23 04:51:04
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/modint>

using Fp = atcoder::modint998244353;
std::ostream& operator<<(std::ostream& os, Fp a) { return os << a.val(); }

std::vector<Fp> fact, ifact;
void reserve(int n) {
  fact.resize(n + 1), ifact.resize(n + 1);
  fact[0] = 1;
  for (int i = 1; i <= n; ++i) fact[i] = i * fact[i - 1];
  ifact[n] = fact[n].pow(Fp::mod() - 2);
  for (int i = n; i; --i) ifact[i - 1] = ifact[i] * i;
}
Fp binom(int n, int k) {
  if (n >= 0) return 0 <= k && k <= n ? fact[n] * ifact[k] * ifact[n - k] : 0;
  if (k >= 0) return k & 1 ? -binom(k + ~n, k) : binom(k + ~n, k);
  return (n ^ k) & 1 ? -binom(~k, ~n) : binom(~k, ~n);
}

int main() {
  using namespace std;
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, c;
  cin >> n >> c;
  vector<int> deg(n);
  for (int _ = n - 1; _--;) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    ++deg[a];
    ++deg[b];
  }
  reserve(n + c);
  vector pw(c + 1, vector<Fp>(n));
  for (int i = 0; i <= c; ++i) {
    pw[i][0] = 1;
    for (int j = 1; j < n; ++j) pw[i][j] = i * pw[i][j - 1];
  }
  Fp ans;
  for (int k = 1; k <= c; ++k) {
    Fp prod = 1;
    for (int d : deg) {
      Fp cur = pw[k][d];
      for (int i = d / 2 + 1; i < d; ++i) cur -= k * binom(d, i) * pw[k - 1][d - i];
      prod *= cur;
    }
    if ((c - k) & 1)
      ans -= prod / pw[k][n - 1] * binom(c, k);
    else
      ans += prod / pw[k][n - 1] * binom(c, k);
  }
  cout << ans << '\n';
}
0