結果

問題 No.758 VVVVV
ユーザー nebukuro09nebukuro09
提出日時 2018-12-06 09:24:49
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,073 bytes
コンパイル時間 1,831 ms
コンパイル使用メモリ 104,520 KB
実行使用メモリ 14,436 KB
最終ジャッジ日時 2023-09-03 21:23:37
合計ジャッジ時間 4,558 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 114 ms
13,360 KB
testcase_04 AC 52 ms
9,560 KB
testcase_05 AC 37 ms
6,140 KB
testcase_06 AC 34 ms
6,620 KB
testcase_07 AC 111 ms
13,364 KB
testcase_08 AC 57 ms
11,348 KB
testcase_09 AC 20 ms
4,380 KB
testcase_10 AC 40 ms
7,256 KB
testcase_11 AC 118 ms
14,344 KB
testcase_12 AC 28 ms
4,608 KB
testcase_13 AC 37 ms
6,956 KB
testcase_14 AC 19 ms
4,380 KB
testcase_15 AC 13 ms
4,384 KB
testcase_16 AC 15 ms
4,376 KB
testcase_17 AC 98 ms
13,188 KB
testcase_18 AC 51 ms
9,236 KB
testcase_19 AC 117 ms
13,324 KB
testcase_20 AC 32 ms
5,424 KB
testcase_21 AC 34 ms
7,212 KB
testcase_22 AC 121 ms
14,436 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 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, core.stdc.string;

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

void main() {
    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;
    }

    if (N == 1) {
        writeln(1);
        return;
    }

    F = new long[](N+1);
    F[0] = 1;
    foreach (i; 1..N+1) F[i] = F[i-1] * i % MOD;

    long leaves = iota(1, N).map!(i => G[i].length == 1).sum;
    long ans = comb(N-1, leaves-1) * comb(N-2, leaves-1) % MOD * powmod(leaves, MOD-2, MOD) % MOD;
    ans.writeln;
}

long comb(long n, long r) {
    return F[n] * powmod(F[n-r], MOD-2, MOD) % MOD * powmod(F[r], MOD-2, MOD) % MOD;
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = a * ret % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0