Skip to content

Commit b145e4a

Browse files
committed
[LeetCode Sync] Runtime - 425 ms (99.36%), Memory - 34 MB (29.16%)
1 parent 9a64214 commit b145e4a

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<p>You are given two integers, <code>m</code> and <code>k</code>, and an integer array <code>nums</code>.</p>
2+
A sequence of integers <code>seq</code> is called <strong>magical</strong> if:
3+
4+
<ul>
5+
<li><code>seq</code> has a size of <code>m</code>.</li>
6+
<li><code>0 &lt;= seq[i] &lt; nums.length</code></li>
7+
<li>The <strong>binary representation</strong> of <code>2<sup>seq[0]</sup> + 2<sup>seq[1]</sup> + ... + 2<sup>seq[m - 1]</sup></code> has <code>k</code> <strong>set bits</strong>.</li>
8+
</ul>
9+
10+
<p>The <strong>array product</strong> of this sequence is defined as <code>prod(seq) = (nums[seq[0]] * nums[seq[1]] * ... * nums[seq[m - 1]])</code>.</p>
11+
12+
<p>Return the <strong>sum</strong> of the <strong>array products</strong> for all valid <strong>magical</strong> sequences.</p>
13+
14+
<p>Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
15+
16+
<p>A <strong>set bit</strong> refers to a bit in the binary representation of a number that has a value of 1.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<div class="example-block">
22+
<p><strong>Input:</strong> <span class="example-io">m = 5, k = 5, nums = [1,10,100,10000,1000000]</span></p>
23+
24+
<p><strong>Output:</strong> <span class="example-io">991600007</span></p>
25+
26+
<p><strong>Explanation:</strong></p>
27+
28+
<p>All permutations of <code>[0, 1, 2, 3, 4]</code> are magical sequences, each with an array product of 10<sup>13</sup>.</p>
29+
</div>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<div class="example-block">
34+
<p><strong>Input:</strong> <span class="example-io">m = 2, k = 2, nums = [5,4,3,2,1]</span></p>
35+
36+
<p><strong>Output:</strong> <span class="example-io">170</span></p>
37+
38+
<p><strong>Explanation:</strong></p>
39+
40+
<p>The magical sequences are <code>[0, 1]</code>, <code>[0, 2]</code>, <code>[0, 3]</code>, <code>[0, 4]</code>, <code>[1, 0]</code>, <code>[1, 2]</code>, <code>[1, 3]</code>, <code>[1, 4]</code>, <code>[2, 0]</code>, <code>[2, 1]</code>, <code>[2, 3]</code>, <code>[2, 4]</code>, <code>[3, 0]</code>, <code>[3, 1]</code>, <code>[3, 2]</code>, <code>[3, 4]</code>, <code>[4, 0]</code>, <code>[4, 1]</code>, <code>[4, 2]</code>, and <code>[4, 3]</code>.</p>
41+
</div>
42+
43+
<p><strong class="example">Example 3:</strong></p>
44+
45+
<div class="example-block">
46+
<p><strong>Input:</strong> <span class="example-io">m = 1, k = 1, nums = [28]</span></p>
47+
48+
<p><strong>Output:</strong> <span class="example-io">28</span></p>
49+
50+
<p><strong>Explanation:</strong></p>
51+
52+
<p>The only magical sequence is <code>[0]</code>.</p>
53+
</div>
54+
55+
<p>&nbsp;</p>
56+
<p><strong>Constraints:</strong></p>
57+
58+
<ul>
59+
<li><code>1 &lt;= k &lt;= m &lt;= 30</code></li>
60+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
61+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>8</sup></code></li>
62+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
MOD = 10**9 + 7
2+
from functools import lru_cache
3+
4+
class Solution:
5+
def magicalSum(self, total_count: int, target_odd: int, numbers: List[int]) -> int:
6+
7+
@lru_cache(None)
8+
def dfs(remaining, odd_needed, index, carry):
9+
if remaining < 0 or odd_needed < 0 or remaining + carry.bit_count() < odd_needed:
10+
return 0
11+
if remaining == 0:
12+
return 1 if odd_needed == carry.bit_count() else 0
13+
if index >= len(numbers):
14+
return 0
15+
16+
ans = 0
17+
for take in range(remaining + 1):
18+
ways = math.comb(remaining, take) * pow(numbers[index], take, MOD) % MOD
19+
new_carry = carry + take
20+
ans += ways * dfs(remaining - take, odd_needed - (new_carry % 2), index + 1, new_carry // 2)
21+
ans %= MOD
22+
return ans
23+
24+
return dfs(total_count, target_odd, 0, 0)

0 commit comments

Comments
 (0)