Skip to content

Commit f42a846

Browse files
committed
Add solution and test-cases for problem 3539
1 parent f03226b commit f42a846

File tree

3 files changed

+133
-24
lines changed

3 files changed

+133
-24
lines changed

leetcode/3501-3600/3539.Find-Sum-of-Array-Product-of-Magical-Sequences/README.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
# [3539.Find Sum of Array Product of Magical Sequences][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two integers, `m` and `k`, and an integer array `nums`.
5+
6+
A sequence of integers `seq` is called **magical** if:
7+
8+
- `seq` has a size of `m`.
9+
- `0 <= seq[i] < nums.length`
10+
- The **binary representation** of `2^seq[0] + 2^seq[1] + ... + 2^seq[m - 1]` has `k` **set bits**.
11+
12+
The **array product** of this sequence is defined as `prod(seq) = (nums[seq[0]] * nums[seq[1]] * ... * nums[seq[m - 1]])`.
13+
14+
Return the **sum** of the **array products** for all valid **magical** sequences.
15+
16+
Since the answer may be large, return it **modulo** `10^9 + 7`.
17+
18+
A **set bit** refers to a bit in the binary representation of a number that has a value of 1.
719

820
**Example 1:**
921

1022
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
23+
nput: m = 5, k = 5, nums = [1,10,100,10000,1000000]
24+
25+
Output: 991600007
26+
27+
Explanation:
28+
29+
All permutations of [0, 1, 2, 3, 4] are magical sequences, each with an array product of 1013.
1330
```
1431

15-
## 题意
16-
> ...
32+
**Example 2:**
1733

18-
## 题解
34+
```
35+
Input: m = 2, k = 2, nums = [5,4,3,2,1]
36+
37+
Output: 170
38+
39+
Explanation:
1940
20-
### 思路1
21-
> ...
22-
Find Sum of Array Product of Magical Sequences
23-
```go
41+
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].
2442
```
2543

44+
**Example 3:**
45+
46+
```
47+
Input: m = 1, k = 1, nums = [28]
48+
49+
Output: 28
50+
51+
Explanation:
52+
53+
The only magical sequence is [0].
54+
```
2655

2756
## 结语
2857

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,84 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "math/bits"
4+
5+
func quickmul(x, y, mod int64) int64 {
6+
res, cur := int64(1), x%mod
7+
for y > 0 {
8+
if y&1 == 1 {
9+
res = res * cur % mod
10+
}
11+
y >>= 1
12+
cur = cur * cur % mod
13+
}
14+
return res
15+
}
16+
17+
func Solution(m int, k int, nums []int) int {
18+
mod := int64(1000000007)
19+
fac := make([]int64, m+1)
20+
fac[0] = 1
21+
for i := 1; i <= m; i++ {
22+
fac[i] = fac[i-1] * int64(i) % mod
23+
}
24+
ifac := make([]int64, m+1)
25+
ifac[0] = 1
26+
ifac[1] = 1
27+
for i := 2; i <= m; i++ {
28+
ifac[i] = quickmul(int64(i), mod-2, mod)
29+
}
30+
for i := 2; i <= m; i++ {
31+
ifac[i] = ifac[i-1] * ifac[i] % mod
32+
}
33+
34+
numsPower := make([][]int64, len(nums))
35+
for i := range nums {
36+
numsPower[i] = make([]int64, m+1)
37+
numsPower[i][0] = 1
38+
for j := 1; j <= m; j++ {
39+
numsPower[i][j] = numsPower[i][j-1] * int64(nums[i]) % mod
40+
}
41+
}
42+
43+
f := make([][][][]int64, len(nums))
44+
for i := range nums {
45+
f[i] = make([][][]int64, m+1)
46+
for j := 0; j <= m; j++ {
47+
f[i][j] = make([][]int64, m*2+1)
48+
for p := 0; p <= m*2; p++ {
49+
f[i][j][p] = make([]int64, k+1)
50+
}
51+
}
52+
}
53+
54+
for j := 0; j <= m; j++ {
55+
f[0][j][j][0] = numsPower[0][j] * ifac[j] % mod
56+
}
57+
for i := 0; i+1 < len(nums); i++ {
58+
for j := 0; j <= m; j++ {
59+
for p := 0; p <= m*2; p++ {
60+
for q := 0; q <= k; q++ {
61+
q2 := p%2 + q
62+
if q2 > k {
63+
break
64+
}
65+
for r := 0; r+j <= m; r++ {
66+
p2 := p/2 + r
67+
f[i+1][j+r][p2][q2] += f[i][j][p][q] * numsPower[i+1][r] % mod * ifac[r] % mod
68+
f[i+1][j+r][p2][q2] %= mod
69+
}
70+
}
71+
}
72+
}
73+
}
74+
75+
res := int64(0)
76+
for p := 0; p <= m*2; p++ {
77+
for q := 0; q <= k; q++ {
78+
if bits.OnesCount(uint(p))+q == k {
79+
res = (res + f[len(nums)-1][m][p][q]*fac[m]%mod) % mod
80+
}
81+
}
82+
}
83+
return int(res)
584
}

leetcode/3501-3600/3539.Find-Sum-of-Array-Product-of-Magical-Sequences/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
m, k int
14+
nums []int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 5, 5, []int{1, 10, 100, 10000, 1000000}, 991600007},
18+
{"TestCase2", 2, 2, []int{5, 4, 3, 2, 1}, 170},
19+
{"TestCase3", 1, 1, []int{28}, 28},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.m, c.k, c.nums)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.m, c.k, c.nums)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)