結果

問題 No.827 総神童数
ユーザー finefine
提出日時 2019-05-03 22:27:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 1,357 bytes
コンパイル時間 1,799 ms
コンパイル使用メモリ 170,836 KB
実行使用メモリ 29,296 KB
最終ジャッジ日時 2023-08-30 06:12:40
合計ジャッジ時間 5,312 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,496 KB
testcase_01 AC 5 ms
6,448 KB
testcase_02 AC 6 ms
6,516 KB
testcase_03 AC 5 ms
6,448 KB
testcase_04 AC 6 ms
6,456 KB
testcase_05 AC 6 ms
6,496 KB
testcase_06 AC 6 ms
6,464 KB
testcase_07 AC 5 ms
6,560 KB
testcase_08 AC 5 ms
6,516 KB
testcase_09 AC 110 ms
29,296 KB
testcase_10 AC 37 ms
13,316 KB
testcase_11 AC 7 ms
6,868 KB
testcase_12 AC 18 ms
9,956 KB
testcase_13 AC 85 ms
25,184 KB
testcase_14 AC 8 ms
7,368 KB
testcase_15 AC 34 ms
13,668 KB
testcase_16 AC 84 ms
20,920 KB
testcase_17 AC 109 ms
24,648 KB
testcase_18 AC 40 ms
15,396 KB
testcase_19 AC 109 ms
17,668 KB
testcase_20 AC 72 ms
14,660 KB
testcase_21 AC 51 ms
12,240 KB
testcase_22 AC 91 ms
15,364 KB
testcase_23 AC 6 ms
6,728 KB
testcase_24 AC 96 ms
16,392 KB
testcase_25 AC 70 ms
14,016 KB
testcase_26 AC 103 ms
16,388 KB
testcase_27 AC 59 ms
12,948 KB
testcase_28 AC 58 ms
12,940 KB
testcase_29 AC 43 ms
11,520 KB
testcase_30 AC 17 ms
7,776 KB
testcase_31 AC 37 ms
10,424 KB
testcase_32 AC 35 ms
10,216 KB
testcase_33 AC 122 ms
17,192 KB
testcase_34 AC 101 ms
16,380 KB
testcase_35 AC 36 ms
10,464 KB
testcase_36 AC 50 ms
12,260 KB
testcase_37 AC 64 ms
13,796 KB
testcase_38 AC 100 ms
17,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

ll modpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

const int MAXV = 200000;
ll f[200005], fi[200005];

vector< vector<int> > g;
void dfs(int cur, int par, vector<int>& dist) {
    for (int nex : g[cur]) {
        if (nex == par) continue;
        dist[nex] = dist[cur] + 1;
        dfs(nex, cur, dist);
    }
}

ll comb(int r, int c) {
    if (r < c) return 0;
    return f[r] * fi[c] % MOD * fi[r - c] % MOD;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    f[0] = 1;
    for (int i = 1; i <= MAXV; i++) {
        f[i] = f[i - 1] * i % MOD;
    }
    fi[MAXV] = modpow(f[MAXV], MOD - 2);
    for (int i = MAXV; i > 0; i--) {
        fi[i-1] = fi[i] * i % MOD;
    }

    int n;
    cin >> n;
    g.resize(n);
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vector<int> dist(n, 0);
    dfs(0, -1, dist);

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        (ans += comb(n, dist[i] + 1) * f[dist[i]] % MOD * f[n - dist[i] - 1] % MOD) %= MOD;
    }
    cout << ans << endl;
    return 0;
}
0