3539. Find Sum of Array Product of Magical Sequences Hard

@problem@discussion
#Array#Math#Dynamic Programming#Bit Manipulation#Combinatorics#Bitmask



1/**
2 * [3539] Find Sum of Array Product of Magical Sequences
3 *
4 * You are given two integers, m and k, and an integer array nums.
5 * A sequence of integers seq is called magical if:
6 * 
7 * 	seq has a size of m.
8 * 	0 <= seq[i] < nums.length
9 * 	The binary representation of 2^seq[0] + 2^seq[1] + ... + 2^seq[m - 1] has k set bits.
10 * 
11 * The array product of this sequence is defined as prod(seq) = (nums[seq[0]] * nums[seq[1]] * ... * nums[seq[m - 1]]).
12 * Return the sum of the array products for all valid magical sequences.
13 * Since the answer may be large, return it modulo 10^9 + 7.
14 * A set bit refers to a bit in the binary representation of a number that has a value of 1.
15 *  
16 * <strong class="example">Example 1:
17 * <div class="example-block">
18 * Input: <span class="example-io">m = 5, k = 5, nums = [1,10,100,10000,1000000]</span>
19 * Output: <span class="example-io">991600007</span>
20 * Explanation:
21 * All permutations of [0, 1, 2, 3, 4] are magical sequences, each with an array product of 10^13.
22 * </div>
23 * <strong class="example">Example 2:
24 * <div class="example-block">
25 * Input: <span class="example-io">m = 2, k = 2, nums = [5,4,3,2,1]</span>
26 * Output: <span class="example-io">170</span>
27 * Explanation:
28 * 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].
29 * </div>
30 * <strong class="example">Example 3:
31 * <div class="example-block">
32 * Input: <span class="example-io">m = 1, k = 1, nums = [28]</span>
33 * Output: <span class="example-io">28</span>
34 * Explanation:
35 * The only magical sequence is [0].
36 * </div>
37 *  
38 * Constraints:
39 * 
40 * 	1 <= k <= m <= 30
41 * 	1 <= nums.length <= 50
42 * 	1 <= nums[i] <= 10^8
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/find-sum-of-array-product-of-magical-sequences/
48// discuss: https://leetcode.com/problems/find-sum-of-array-product-of-magical-sequences/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53    pub fn magical_sum(m: i32, k: i32, nums: Vec<i32>) -> i32 {
54        0
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_3539() {
66    }
67}
68

Back
© 2026 bowen.ge All Rights Reserved.