結果

問題 No.827 総神童数
ユーザー nebukuro09nebukuro09
提出日時 2019-05-03 22:00:41
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 379 ms / 2,000 ms
コード長 1,328 bytes
コンパイル時間 709 ms
コンパイル使用メモリ 102,684 KB
実行使用メモリ 35,784 KB
最終ジャッジ日時 2023-09-04 01:03:44
合計ジャッジ時間 11,507 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
7,680 KB
testcase_01 AC 119 ms
7,224 KB
testcase_02 AC 119 ms
7,512 KB
testcase_03 AC 119 ms
7,160 KB
testcase_04 AC 118 ms
7,360 KB
testcase_05 AC 119 ms
7,104 KB
testcase_06 AC 118 ms
7,428 KB
testcase_07 AC 118 ms
7,052 KB
testcase_08 AC 119 ms
6,000 KB
testcase_09 AC 349 ms
35,784 KB
testcase_10 AC 191 ms
19,276 KB
testcase_11 AC 123 ms
8,308 KB
testcase_12 AC 151 ms
17,356 KB
testcase_13 AC 297 ms
30,888 KB
testcase_14 AC 126 ms
9,388 KB
testcase_15 AC 185 ms
20,516 KB
testcase_16 AC 296 ms
24,552 KB
testcase_17 AC 334 ms
30,152 KB
testcase_18 AC 207 ms
21,312 KB
testcase_19 AC 379 ms
28,080 KB
testcase_20 AC 301 ms
15,836 KB
testcase_21 AC 244 ms
15,240 KB
testcase_22 AC 322 ms
16,828 KB
testcase_23 AC 121 ms
7,288 KB
testcase_24 AC 342 ms
22,720 KB
testcase_25 AC 279 ms
15,760 KB
testcase_26 AC 340 ms
23,952 KB
testcase_27 AC 258 ms
15,324 KB
testcase_28 AC 256 ms
15,308 KB
testcase_29 AC 225 ms
14,988 KB
testcase_30 AC 152 ms
13,608 KB
testcase_31 AC 201 ms
14,524 KB
testcase_32 AC 197 ms
14,992 KB
testcase_33 AC 362 ms
28,448 KB
testcase_34 AC 338 ms
22,768 KB
testcase_35 AC 204 ms
14,512 KB
testcase_36 AC 239 ms
15,036 KB
testcase_37 AC 281 ms
15,768 KB
testcase_38 AC 352 ms
26,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

immutable long MOD = 10^^9 + 7;
long[] F, G;

void main() {
    F = new long[](202020);
    G = new long[](202020);
    F[0] = F[1] = 1;
    foreach (i; 2..202020) F[i] = i * F[i-1] % MOD;
    foreach (i; 0..202020) G[i] = powmod(F[i], MOD-2, MOD);

    auto N = readln.chomp.to!int;
    auto G = new int[][](N);
    foreach (_; 0..N-1) {
        auto s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }
    auto depth = new int[](N);

    void dfs(int n, int p, int d) {
        depth[n] = d;
        foreach (m; G[n]) {
            if (m == p) continue;
            dfs(m, n, d+1);
        }
    }

    dfs(0, -1, 0);
    long ans = 0;

    foreach (i; 0..N) {
        int m = depth[i] + 1;
        (ans += comb(N, m) * F[m-1] % MOD * F[N-depth[i]-1] % MOD) %= MOD;
    }        

    ans.writeln;
}

long comb(long n, long k) {
    if (k > n) return 0;
    return F[n.to!int] * G[n.to!int-k.to!int] % MOD * G[k.to!int] % MOD;
}
 
long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0