Skip to content

Commit ef8430e

Browse files
committed
[LeetCode Sync] Runtime - 1787 ms (51.15%), Memory - 55.7 MB (7.86%)
1 parent 5e960ca commit ef8430e

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution:
2+
def mul_with_mod(self, x: int, y: int, mod: int) -> int:
3+
result, current = 1, x % mod
4+
while y:
5+
if y & 1:
6+
result = result * current % mod
7+
y >>= 1
8+
current = current * current % mod
9+
return result
10+
11+
def magicalSum(self, m: int, k: int, nums: List[int]) -> int:
12+
n = len(nums)
13+
mod = 10**9 + 7
14+
15+
fac = [1] * (m + 1)
16+
for i in range(1, m + 1):
17+
fac[i] = fac[i - 1] * i % mod
18+
19+
ifac = [1] * (m + 1)
20+
for i in range(2, m + 1):
21+
ifac[i] = self.mul_with_mod(i, mod - 2, mod)
22+
for i in range(2, m + 1):
23+
ifac[i] = ifac[i - 1] * ifac[i] % mod
24+
25+
numsPower = [[1] * (m + 1) for _ in range(n)]
26+
for i in range(n):
27+
for j in range(1, m + 1):
28+
numsPower[i][j] = numsPower[i][j - 1] * nums[i] % mod
29+
30+
dp = [[[[0] * (k + 1) for _ in range(m * 2 + 1)] for _ in range(m + 1)] for _ in range(n)]
31+
32+
for j in range(m + 1):
33+
dp[0][j][j][0] = numsPower[0][j] * ifac[j] % mod
34+
35+
for i in range(n - 1):
36+
for j in range(m + 1):
37+
for p in range(m * 2 + 1):
38+
for q in range(k + 1):
39+
if dp[i][j][p][q] == 0:
40+
continue
41+
q2 = (p % 2) + q
42+
43+
if q2 > k:
44+
break
45+
46+
for r in range(m - j + 1):
47+
p2 = (p // 2) + r
48+
if p2 > m * 2:
49+
continue
50+
dp[i + 1][j + r][p2][q2] = (dp[i + 1][j + r][p2][q2] + dp[i][j][p][q]
51+
* numsPower[i + 1][r] % mod * ifac[r] % mod
52+
) % mod
53+
54+
result = 0
55+
for p in range(m * 2 + 1):
56+
for q in range(k + 1):
57+
if bin(p).count("1") + q == k:
58+
result = (result + dp[n - 1][m][p][q] * fac[m] % mod) % mod
59+
60+
return result

0 commit comments

Comments
 (0)