3539. Find Sum of Array Product of Magical Sequences
Description
You are given two integers, m and k, and an integer array nums.
A sequence of integers seq is called magical if:
seqhas a size ofm.0 <= seq[i] < nums.length- The binary representation of
2seq[0] + 2seq[1] + ... + 2seq[m - 1]haskset bits.
The array product of this sequence is defined as prod(seq) = (nums[seq[0]] * nums[seq[1]] * ... * nums[seq[m - 1]]).
Return the sum of the array products for all valid magical sequences.
Since the answer may be large, return it modulo 109 + 7.
A set bit refers to a bit in the binary representation of a number that has a value of 1.
Example 1:
Input: m = 5, k = 5, nums = [1,10,100,10000,1000000]
Output: 991600007
Explanation:
All permutations of [0, 1, 2, 3, 4] are magical sequences, each with an array product of 1013.
Example 2:
Input: m = 2, k = 2, nums = [5,4,3,2,1]
Output: 170
Explanation:
The magical sequences are [0, 1], [0, 2], [0, 3], [0, 4], [1, 0], [1, 2], [1, 3], [1, 4], [2, 0], [2, 1], [2, 3], [2, 4], [3, 0], [3, 1], [3, 2], [3, 4], [4, 0], [4, 1], [4, 2], and [4, 3].
Example 3:
Input: m = 1, k = 1, nums = [28]
Output: 28
Explanation:
The only magical sequence is [0].
Constraints:
1 <= k <= m <= 301 <= nums.length <= 501 <= nums[i] <= 108
Solutions
Solution 1: Combinatorics + Memoized Search
Thinking
A sequence of length \(m\) draws from \(\textit{nums}\); the product’s contribution depends on the popcount of the frequency vector after binary carrying. Enumerating sequences is impossible.
Assign a multiplicity \(t\) to \(\textit{nums}[i]\) with weight \(\binom{j}{t} \cdot \textit{nums}[i]^t\). The carry and remaining popcount update from \(t+\textit{st}\). Memoize \(\textit{dfs}(i,j,k,\textit{st})\) and invert factorials for the binomials.
We design a function \(\text{dfs}(i, j, k, st)\), which represents the number of ways when we are currently processing the \(i\)-th element of array \(\textit{nums}\), still need to select numbers from the remaining \(j\) positions to fill into the magical sequence, still need to satisfy having \(k\) set bits in binary form, and the current carry from the previous bit is \(st\). Then the answer is \(\text{dfs}(0, m, k, 0)\).
The execution process of function \(\text{dfs}(i, j, k, st)\) is as follows:
If \(k < 0\) or \(i = n\) and \(j > 0\), it means the current solution is not feasible, return \(0\).
If \(i = n\), it means we have finished processing array \(\textit{nums}\). We need to check if there are still set bits in the current carry \(st\), and if so, we need to decrease \(k\). If \(k = 0\) at this point, it means the current solution is feasible, return \(1\), otherwise return \(0\).
Otherwise, we enumerate selecting \(t\) numbers at position \(i\) to fill into the magical sequence (\(0 \leq t \leq j\)). The number of ways to fill \(t\) numbers into the magical sequence is \(\binom{j}{t}\), the array product is \(\textit{nums}[i]^t\), the updated carry is \((t + st) >> 1\), the updated number of required set bits is \(k - ((t + st) \& 1)\), and we recursively call \(\text{dfs}(i + 1, j - t, k - ((t + st) \& 1), (t + st) >> 1)\). The sum of all \(t\) solutions is \(\text{dfs}(i, j, k, st)\).
To efficiently compute the binomial coefficient \(\binom{m}{n}\), we preprocess the factorial array \(f\) and the inverse factorial array \(g\), where \(f[i] = i! \mod (10^9 + 7)\) and \(g[i] = (i!)^{-1} \mod (10^9 + 7)\). Then \(\binom{m}{n} = f[m] \cdot g[n] \cdot g[m - n] \mod (10^9 + 7)\).
The time complexity is \(O(n \cdot m^3 \cdot k)\) and the space complexity is \(O(n \cdot m^2 \cdot k)\), where \(n\) is the length of array \(\textit{nums}\), and \(m\) and \(k\) are the parameters in the problem.
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 | |
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 | |
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 | |
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 | |