結果

問題 No.827 総神童数
ユーザー PachicobuePachicobue
提出日時 2019-04-11 00:42:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,074 bytes
コンパイル時間 850 ms
コンパイル使用メモリ 78,812 KB
最終ジャッジ日時 2025-01-07 01:37:42
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 2 RE * 1
other AC * 1 WA * 18 RE * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using ull = unsigned long long;
int main()
{
    std::size_t N;
    std::cin >> N;
    ull M;
    std::cin >> M;
    std::vector<ull> L(N + 1, 1), R(N + 1, 1);
    for (std::size_t i = 1; i <= N; i++) { L[i] = L[i - 1] * (ull)(i == 1 ? 1 : i - 1) % M; }
    for (std::size_t i = N; i >= 1; i--) { R[i - 1] = R[i] * (ull)(i) % M; }
    std::vector<std::vector<std::size_t>> g(N);
    for (std::size_t i = 0; i < N - 1; i++) {
        std::size_t u, v;
        std::cin >> u >> v, u--, v--;
        g[u].push_back(v), g[v].push_back(u);
    }
    std::vector<std::size_t> dnum(N + 1, 0);
    auto dfs = [&](auto&& self, const std::size_t s, const std::size_t p, const std::size_t d) -> void {
        dnum[d]++;
        for (const std::size_t to : g[s]) {
            if (to == p) { continue; }
            self(self, to, s, d + 1);
        }
    };
    dfs(dfs, 0, N, 1);
    ull ans = 0;
    for (std::size_t d = 1; d <= N; d++) { (ans += (ull)dnum[d] * (L[d] * R[d] % M) % M) %= M; }
    std::cout << ans << std::endl;
    return 0;
}
0