File tree Expand file tree Collapse file tree 1 file changed +13
-14
lines changed Expand file tree Collapse file tree 1 file changed +13
-14
lines changed Original file line number Diff line number Diff line change 1
1
# Time: O(1), excluding precomputation time
2
2
# Space: O(n)
3
3
4
- class Solution (object ):
5
- def __init__ (self ):
6
- MOD = 10 ** 9 + 7
7
- MAX_N = 1000
8
- self .__fact = [0 ]* (2 * MAX_N - 1 + 1 )
9
- self .__inv = [0 ]* (2 * MAX_N - 1 + 1 )
10
- self .__inv_fact = [0 ]* (2 * MAX_N - 1 + 1 )
11
- self .__fact [0 ] = self .__inv_fact [0 ] = self .__fact [1 ] = self .__inv_fact [1 ] = self .__inv [1 ] = 1
12
- for i in xrange (2 , len (self .__fact )):
13
- self .__fact [i ] = self .__fact [i - 1 ]* i % MOD
14
- self .__inv [i ] = self .__inv [MOD % i ]* (MOD - MOD // i ) % MOD # https://cp-algorithms.com/algebra/module-inverse.html
15
- self .__inv_fact [i ] = self .__inv_fact [i - 1 ]* self .__inv [i ] % MOD
4
+ # precompute
5
+ MOD = 10 ** 9 + 7
6
+ MAX_N = 1000
7
+ fact = [0 ]* (2 * MAX_N - 1 + 1 )
8
+ inv = [0 ]* (2 * MAX_N - 1 + 1 )
9
+ inv_fact = [0 ]* (2 * MAX_N - 1 + 1 )
10
+ fact [0 ] = inv_fact [0 ] = fact [1 ] = inv_fact [1 ] = inv [1 ] = 1
11
+ for i in xrange (2 , len (fact )):
12
+ fact [i ] = fact [i - 1 ]* i % MOD
13
+ inv [i ] = inv [MOD % i ]* (MOD - MOD // i ) % MOD # https://cp-algorithms.com/algebra/module-inverse.html
14
+ inv_fact [i ] = inv_fact [i - 1 ]* inv [i ] % MOD
16
15
16
+ class Solution (object ):
17
17
def numberOfSets (self , n , k ):
18
18
"""
19
19
:type n: int
20
20
:type k: int
21
21
:rtype: int
22
22
"""
23
- MOD = 10 ** 9 + 7
24
23
def nCr (n , k , mod ):
25
- return (self . __fact [n ]* self . __inv_fact [n - k ] % mod ) * self . __inv_fact [k ] % mod
24
+ return (fact [n ]* inv_fact [n - k ] % mod ) * inv_fact [k ] % mod
26
25
27
26
# find k segments with 1+ length and (k+1) spaces with 0+ length s.t. total length is n-1
28
27
# => find k segments with 0+ length and (k+1) spaces with 0+ length s.t. total length is n-k-1
You can’t perform that action at this time.
0 commit comments