- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 46 lines of Python from the credited upstream file 1735.py.
- The implementation visibly relies on sequence storage, hash lookup.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def waysToFillArray(self, queries: list[list[int]]) -> list[int]:3 MOD = 1_000_000_0074 MAX = 10_0005 minPrimeFactors = self._sieveEratosthenes(MAX + 1)6 7 @functools.lru_cache(None)8 def fact(i: int) -> int:9 return 1 if i <= 1 else i * fact(i - 1) % MOD10 11 @functools.lru_cache(None)12 def inv(i: int) -> int:13 return pow(i, MOD - 2, MOD)14 15 @functools.lru_cache(None)16 def nCk(n: int, k: int) -> int:17 return fact(n) * inv(fact(k)) * inv(fact(n - k)) % MOD18 19 ans = []20 21 for n, k in queries:22 res = 123 for freq in self._getPrimeFactorsCount(k, minPrimeFactors).values():24 res = res * nCk(n - 1 + freq, freq) % MOD25 ans.append(res)26 27 return ans28 29 def _sieveEratosthenes(self, n: int) -> list[int]:30 """Gets the minimum prime factor of i, where 1 < i <= n."""31 minPrimeFactors = [i for i in range(n + 1)]32 for i in range(2, int(n**0.5) + 1):33 if minPrimeFactors[i] == i: 34 for j in range(i * i, n, i):35 minPrimeFactors[j] = min(minPrimeFactors[j], i)36 return minPrimeFactors37 38 def _getPrimeFactorsCount(self, num: int, minPrimeFactors: list[int]) -> dict[int, int]:39 count = collections.Counter()40 while num > 1:41 divisor = minPrimeFactors[num]42 while num % divisor == 0:43 num = divisor44 count[divisor] += 145 return count46