forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-the-number-of-infection-sequences.py
31 lines (29 loc) · 1.08 KB
/
count-the-number-of-infection-sequences.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
# Time: precompute: O(max_n)
# runtime: O(s + logn)
# Space: O(max_n)
# combinatorics
FACT, INV, INV_FACT = [[1]*2 for _ in xrange(3)]
class Solution(object):
def numberOfSequence(self, n, sick):
"""
:type n: int
:type sick: List[int]
:rtype: int
"""
MOD = 10**9+7
def nCr(n, k):
while len(INV) <= n: # lazy initialization
FACT.append(FACT[-1]*len(INV) % MOD)
INV.append(INV[MOD%len(INV)]*(MOD-MOD//len(INV)) % MOD) # https://cp-algorithms.com/algebra/module-INVerse.html
INV_FACT.append(INV_FACT[-1]*INV[-1] % MOD)
return (FACT[n]*INV_FACT[n-k] % MOD) * INV_FACT[k] % MOD
result = 1
total = cnt = 0
for i in xrange(len(sick)+1):
l = (sick[i] if i < len(sick) else n)-(sick[i-1] if i-1 >= 0 else -1)-1
if i not in (0, len(sick)):
cnt += max(l-1, 0)
total += l
result = (result*nCr(total, l))%MOD
result = (result*pow(2, cnt, MOD))%MOD
return result