Problem Description
Given two integers n and maxValue, count the number of distinct “ideal” arrays of length n such that:
- Every element is between
1andmaxValue. - For every
i > 0,arr[i] % arr[i-1] == 0.
Return the count modulo 10⁹+7.
Key Insights
- Any ideal array can be seen as choosing a strictly increasing divisibility‐chain of distinct values
[v₁∣v₂∣…∣vₖ], then “spreading” thosekvalues (with repetitions) across thenslots. - We first count, for each end‑value
vand chain‑lengthk, the number of divisor‑chains ending atvof lengthk. - Once a chain of length
kis chosen, the number of ways to intersperse itskblocks into thenpositions is
(\displaystyle \binom{n-1}{k-1}). - The maximum distinct‑value chain‑length is only about (\lfloor\log₂(\text{maxValue})\rfloor+1), so both DP and combinatorics stay efficient.
Space and Time Complexity
- Time:
- DP “sieve” over
v=1…maxValue, extending each chain to its multiples takes
(\sum_{v=1}^{\text{maxValue}}(\tfrac{\text{maxValue}}{v}\cdot L)=O(\text{maxValue}·\log\text{maxValue}·L)). - Precomputing combinations (\binom{s}{k}) for
s=0…n-1,k=0…L-1is (O(n·L)). - Final sum is (O(\text{maxValue}·L)).
- DP “sieve” over
- Space:
cnt[maxValue+1][L]for DP, andcomb[n][L]for binomials → (O(\text{maxValue}·L + n·L)).
Solution
- DP‑build chains
Letcnt[v][k]= number of strictly‑increasing divisor‑chains of lengthk+1that end atv.- Initialize
cnt[v][0]=1(the chain[v]). - For each
vand eachk<L-1, addcnt[v][k]intocnt[m][k+1]for every multiplem=v*2, v*3,…≤maxValue.
- Initialize
- Precompute binomials
Build Pascal’s trianglecomb[s][k] = C(s, k)for0≤s<n,0≤k<L. - Aggregate answer
[ \sum_{v=1}^{\text{maxValue}};\sum_{k=0}^{\min(L-1,n-1)} \text{cnt}[v][k];\times;\binom{n-1}{k} ;\bmod 10^{9}+7. ]