Count the Number of Ideal Arrays Problem
Description
LeetCode Problem 2338.
You are given two integers n and maxValue, which are used to describe an ideal array.
A 0-indexed integer array arr of length n is considered ideal if the following conditions hold:
- Every
arr[i]is a value from1tomaxValue, for0 <= i < n. - Every
arr[i]is divisible byarr[i - 1], for0 < i < n.
Return the number of distinct ideal arrays of length n. Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:
Input: n = 2, maxValue = 5
Output: 10
Explanation: The following are the possible ideal arrays:
- Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]
- Arrays starting with the value 2 (2 arrays): [2,2], [2,4]
- Arrays starting with the value 3 (1 array): [3,3]
- Arrays starting with the value 4 (1 array): [4,4]
- Arrays starting with the value 5 (1 array): [5,5]
There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays.
Example 2:
Input: n = 5, maxValue = 3
Output: 11
Explanation: The following are the possible ideal arrays:
- Arrays starting with the value 1 (9 arrays):
- With no other distinct values (1 array): [1,1,1,1,1]
- With 2^nd distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]
- With 2^nd distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]
- Arrays starting with the value 2 (1 array): [2,2,2,2,2]
- Arrays starting with the value 3 (1 array): [3,3,3,3,3]
There are a total of 9 + 1 + 1 = 11 distinct ideal arrays.
Constraints:
2 <= n <= 10^41 <= maxValue <= 10^4
Solution
Approach: Dynamic Programming + Combinatorics
In an ideal array every element divides the next one. Look at the distinct values in order: each is at least twice the previous one, so there are at most log2(maxValue) + 1 <= 14 of them (we use 15 as a safe bound).
Let f[i][j] be the number of strictly increasing divisibility chains of j distinct values that end at i. Start with f[i][1] = 1, and for each i, j and multiple k * i <= maxValue with k >= 2, add f[i][j] to f[k * i][j + 1].
An array of length n with j distinct values is a chain plus a way to split the n positions into j non-empty groups, which is C(n - 1, j - 1) (choose where the j - 1 value changes happen). Precompute C with Pascal's rule, only up to j = 15. The answer is the sum over all i and j of f[i][j] * C(n - 1, j - 1) modulo 10^9 + 7.
Complexity: Time O(n log m + m log^2 m), space O(n log m + m log m), where m = maxValue.
class Solution:
def idealArrays(self, n: int, maxValue: int) -> int:
c = [[0] * 16 for _ in range(n)]
mod = 10**9 + 7
for i in range(n):
for j in range(min(16, i + 1)):
c[i][j] = 1 if j == 0 else (c[i - 1][j] + c[i - 1][j - 1]) % mod
f = [[0] * 16 for _ in range(maxValue + 1)]
for i in range(1, maxValue + 1):
f[i][1] = 1
for j in range(1, 15):
for i in range(1, maxValue + 1):
k = 2
while k * i <= maxValue:
f[k * i][j + 1] = (f[k * i][j + 1] + f[i][j]) % mod
k += 1
ans = 0
for i in range(1, maxValue + 1):
for j in range(1, 16):
ans = (ans + f[i][j] * c[-1][j - 1]) % mod
return ans