結果

問題 No.758 VVVVV
コンテスト
ユーザー nebukuro09
提出日時 2018-12-06 09:24:49
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,073 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 384 ms
コンパイル使用メモリ 101,100 KB
実行使用メモリ 15,196 KB
最終ジャッジ日時 2026-03-05 21:28:29
合計ジャッジ時間 1,957 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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