-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.py
66 lines (61 loc) · 1.55 KB
/
c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from functools import lru_cache
from collections import defaultdict
from queue import deque
M=1000000007
@lru_cache
def mul(a,b):
return a*b%M
@lru_cache
def eeuclid(a,b):
xx,yy,x,y=0,1,1,0
while b:
q=a//b
a,b=b,a%b
xx,x=x-q*xx,xx
yy,y=y-q*yy,yy
return a,x,y
@lru_cache
def inv(x):
a,k1,k2=eeuclid(x,M)
return (k1%M)
@lru_cache
def factorial(x):
if x==0: return 1
return mul(x,factorial(x-1))
def work(S):
q=deque()
childrenof=defaultdict(set)
for pid,vc in enumerate(S):
if vc>=len(q)+2 or vc<=0: return 0
topopcount=len(q)+1-vc
lastpopped=None
for _ in range(topopcount):
lastpopped=q.popleft()
if q:
childrenof[q[0]].add(pid)
if lastpopped!=None:
childrenof[pid].add(lastpopped)
if lastpopped!=None and q:
childrenof[q[0]].remove(lastpopped)
q.appendleft(pid)
root=q.pop()
def explore(cur):
cn=len(childrenof[cur])
subcount = [0]*cn
subans = [0]*cn
for ci,child in enumerate(childrenof[cur]):
subcount[ci],subans[ci] = explore(child)
totchild=sum(subcount)
ans=factorial(totchild)
for ci in range(cn):
ans=mul(ans,subans[ci])
ans=mul(ans,inv(factorial(subcount[ci])))
return totchild+1,ans
totcount,ans=explore(root)
return ans
tn=int(input())
for ti in range(tn):
n=int(input())
S=[int(x) for x in input().split()]
ans=work(S)
print(f'Case #{ti+1}: {ans}')