結果

問題 No.827 総神童数
ユーザー kohei2019kohei2019
提出日時 2022-04-21 01:04:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 601 ms / 2,000 ms
コード長 1,487 bytes
コンパイル時間 3,009 ms
コンパイル使用メモリ 86,392 KB
実行使用メモリ 146,352 KB
最終ジャッジ日時 2023-09-02 22:47:58
合計ジャッジ時間 15,688 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
71,212 KB
testcase_01 AC 105 ms
71,212 KB
testcase_02 AC 114 ms
71,448 KB
testcase_03 AC 106 ms
71,072 KB
testcase_04 AC 107 ms
71,564 KB
testcase_05 AC 106 ms
71,572 KB
testcase_06 AC 110 ms
71,076 KB
testcase_07 AC 107 ms
71,528 KB
testcase_08 AC 105 ms
71,468 KB
testcase_09 AC 582 ms
144,628 KB
testcase_10 AC 281 ms
99,884 KB
testcase_11 AC 155 ms
78,600 KB
testcase_12 AC 204 ms
86,272 KB
testcase_13 AC 443 ms
130,080 KB
testcase_14 AC 154 ms
79,496 KB
testcase_15 AC 257 ms
97,132 KB
testcase_16 AC 442 ms
130,696 KB
testcase_17 AC 508 ms
138,896 KB
testcase_18 AC 282 ms
104,888 KB
testcase_19 AC 601 ms
146,352 KB
testcase_20 AC 476 ms
126,136 KB
testcase_21 AC 372 ms
113,180 KB
testcase_22 AC 509 ms
131,552 KB
testcase_23 AC 144 ms
78,840 KB
testcase_24 AC 564 ms
125,764 KB
testcase_25 AC 453 ms
123,816 KB
testcase_26 AC 571 ms
125,952 KB
testcase_27 AC 411 ms
115,516 KB
testcase_28 AC 399 ms
116,312 KB
testcase_29 AC 347 ms
107,868 KB
testcase_30 AC 201 ms
85,504 KB
testcase_31 AC 307 ms
101,612 KB
testcase_32 AC 285 ms
100,560 KB
testcase_33 AC 576 ms
140,888 KB
testcase_34 AC 555 ms
137,904 KB
testcase_35 AC 310 ms
102,144 KB
testcase_36 AC 385 ms
113,156 KB
testcase_37 AC 448 ms
123,448 KB
testcase_38 AC 578 ms
139,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
class NCK:
    '''
    最大値 :N
    mod
    '''
    def __init__(self,N,mod):
        self.mod = mod
        self.N = N
        self.factmod = [1,1]
        for i in range(2,self.N+1):
            self.factmod.append((self.factmod[-1]*i)%self.mod)

        self.inv = [0,1]
        for i in range(2, N + 1):
            self.inv.append((-self.inv[self.mod % i] * (self.mod // i)) % self.mod)

        self.invfact = [1,1]
        for i in range(2, N + 1):
            self.invfact.append((self.invfact[-1]*self.inv[i])%self.mod)

    def nCk(self,a,b):
        if a < b:
            return 0
        return ((self.factmod[a]*self.invfact[b]%self.mod)*self.invfact[a-b])%self.mod
    
    def invnCk(self,a,b):
        return ((self.factmod[a-b]*self.invfact[a]%self.mod)*self.factmod[b])%self.mod

N = int(input())
lsg = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    lsg[a].append(b)
    lsg[b].append(a)
INF = float('INF')
cost = [INF]*(N)
used = [False]*(N)
cost[0] = 0
d = collections.deque([0])
while d:
    n = d.popleft()
    if used[n]:
        continue
    used[n] = True
    for j in lsg[n]:
        if used[j]:
            continue
        if cost[j] > cost[n]+1:
            cost[j] = cost[n]+1
            d.append(j)
mod = 10**9+7
nck = NCK(N+1, mod)
ans = 0
for i in range(N):
    dist = cost[i]
    ans += nck.nCk(N, dist+1)*nck.factmod[dist]*nck.factmod[N-dist-1]
    ans %= mod
print(ans)
0