-
Notifications
You must be signed in to change notification settings - Fork 17
/
count-ways-to-make-array-with-product.py
111 lines (76 loc) · 2.82 KB
/
count-ways-to-make-array-with-product.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from functools import lru_cache
from typing import List, Set
def sieve(limit: int = 10001) -> List[int]:
primes: Set[int] = set(range(2, limit))
for num in range(2, limit):
if num in primes:
total = num
while total + num < limit:
total += num
primes.discard(total)
return list(sorted(primes))
class Solution:
def waysToFillArray(self, queries: List[List[int]]) -> List[int]:
primes = sieve()
result: List[int] = []
@lru_cache(None)
def backtrack(items: int, buckets: int) -> int:
if buckets == 1:
return 1
if items == 0:
return 1
if items == 1:
return buckets
count = 0
for new_items in range(items + 1):
count += backtrack(new_items, buckets - 1)
return count
for n, k in queries:
# Handle the situation number itself is prime
count = 1 if k not in primes else n
for prime in primes:
if prime >= k // 2 + 1:
break
if k % prime == 0:
repeats = 0
left = k
while left % prime == 0:
repeats += 1
left //= prime
count *= backtrack(repeats, n)
result.append(count % (10**9 + 7))
return result
def waysToFillArrayBruteForce(self, queries: List[List[int]]) -> List[int]:
primes = sieve()
primes_set = set(primes)
@lru_cache(None)
def get_prime_divisors(number: int, prime: int) -> Set[int]:
divisors: Set[int] = set()
for divisor in range(prime, number // 2 + 1, prime):
if number % divisor == 0:
divisors.add(divisor)
if divisor != number // divisor:
divisors.add(number // divisor)
return divisors
@lru_cache(None)
def get_divisors(number: int) -> Set[int]:
if number == 1:
return {1}
divisors: Set[int] = {1, number}
for prime in primes:
if prime > number:
break
divisors |= get_prime_divisors(number, prime)
return divisors
@lru_cache(None)
def backtrack(number: int, left: int) -> int:
if left == 1:
return 1
if number in primes_set:
return left
count = 0
divisors = get_divisors(number)
for divisor in divisors:
count += backtrack(number // divisor, left - 1)
return count % (10**9 + 7)
return [backtrack(k, n) for n, k in queries]