結果

問題 No.1614 Majority Painting on Tree
ユーザー risujirohrisujiroh
提出日時 2021-07-21 22:43:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 445 ms / 5,000 ms
コード長 1,498 bytes
コンパイル時間 2,314 ms
コンパイル使用メモリ 208,204 KB
実行使用メモリ 105,420 KB
最終ジャッジ日時 2023-09-24 19:00:32
合計ジャッジ時間 11,271 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 312 ms
74,784 KB
testcase_01 AC 109 ms
26,308 KB
testcase_02 AC 151 ms
35,588 KB
testcase_03 AC 254 ms
59,480 KB
testcase_04 AC 318 ms
74,864 KB
testcase_05 AC 301 ms
71,716 KB
testcase_06 AC 442 ms
105,420 KB
testcase_07 AC 313 ms
74,392 KB
testcase_08 AC 168 ms
39,472 KB
testcase_09 AC 246 ms
57,328 KB
testcase_10 AC 78 ms
18,616 KB
testcase_11 AC 287 ms
68,320 KB
testcase_12 AC 315 ms
75,188 KB
testcase_13 AC 227 ms
54,516 KB
testcase_14 AC 445 ms
104,092 KB
testcase_15 AC 426 ms
100,000 KB
testcase_16 AC 113 ms
27,396 KB
testcase_17 AC 206 ms
49,764 KB
testcase_18 AC 153 ms
35,584 KB
testcase_19 AC 71 ms
17,384 KB
testcase_20 AC 324 ms
78,260 KB
testcase_21 AC 331 ms
80,380 KB
testcase_22 AC 352 ms
86,188 KB
testcase_23 AC 54 ms
13,408 KB
testcase_24 AC 326 ms
78,744 KB
testcase_25 AC 89 ms
21,556 KB
testcase_26 AC 203 ms
49,984 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 3 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 2 ms
4,384 KB
testcase_32 AC 2 ms
4,384 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,384 KB
testcase_36 AC 1 ms
4,384 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,384 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 1 ms
4,384 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 1 ms
4,384 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,384 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 1 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

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