結果

問題 No.827 総神童数
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-05 15:08:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,396 ms / 2,000 ms
コード長 1,455 bytes
コンパイル時間 127 ms
コンパイル使用メモリ 10,960 KB
実行使用メモリ 85,476 KB
最終ジャッジ日時 2023-10-13 02:18:36
合計ジャッジ時間 24,555 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,152 KB
testcase_01 AC 17 ms
8,188 KB
testcase_02 AC 17 ms
8,128 KB
testcase_03 AC 16 ms
8,048 KB
testcase_04 AC 17 ms
8,180 KB
testcase_05 AC 16 ms
8,116 KB
testcase_06 AC 17 ms
8,184 KB
testcase_07 AC 16 ms
8,180 KB
testcase_08 AC 16 ms
8,244 KB
testcase_09 AC 1,396 ms
85,476 KB
testcase_10 AC 442 ms
32,156 KB
testcase_11 AC 34 ms
9,504 KB
testcase_12 AC 189 ms
20,400 KB
testcase_13 AC 1,092 ms
72,372 KB
testcase_14 AC 50 ms
10,836 KB
testcase_15 AC 408 ms
33,604 KB
testcase_16 AC 1,099 ms
60,468 KB
testcase_17 AC 1,384 ms
70,012 KB
testcase_18 AC 551 ms
39,348 KB
testcase_19 AC 1,293 ms
68,244 KB
testcase_20 AC 918 ms
51,048 KB
testcase_21 AC 650 ms
39,576 KB
testcase_22 AC 1,016 ms
56,308 KB
testcase_23 AC 22 ms
8,624 KB
testcase_24 AC 1,094 ms
59,824 KB
testcase_25 AC 815 ms
47,400 KB
testcase_26 AC 1,109 ms
59,852 KB
testcase_27 AC 724 ms
42,984 KB
testcase_28 AC 724 ms
42,340 KB
testcase_29 AC 576 ms
35,612 KB
testcase_30 AC 168 ms
16,868 KB
testcase_31 AC 444 ms
29,640 KB
testcase_32 AC 430 ms
28,792 KB
testcase_33 AC 1,249 ms
64,892 KB
testcase_34 AC 1,114 ms
60,856 KB
testcase_35 AC 451 ms
29,792 KB
testcase_36 AC 663 ms
38,808 KB
testcase_37 AC 829 ms
47,352 KB
testcase_38 AC 1,237 ms
64,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 7)

class Combination:
    def __init__(self, size, mod=10**9 + 7):
        self.size = size + 2
        self.mod = mod
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % self.mod
            self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.mod

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod

    def factN(self, n):
        if n < 0:
            return 0
        return self.fact[n]

N = int(input())
MOD = 10**9 + 7
edges = [[] for _ in range(N)]

for _ in range(N - 1):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)

depth = [-1] * N

def dfs(now, parent, d):
    depth[now] = d
    for to in edges[now]:
        if to == parent:
            continue
        dfs(to, now, d + 1)

dfs(0, -1, 0)

comb = Combination(N + 10)
ans = 0
for i in range(N):
    ans += comb.factN(N) * comb.inv[depth[i] + 1]
    ans %= MOD

print(ans)
0