File tree Expand file tree Collapse file tree 1 file changed +22
-32
lines changed
solution/2300-2399/2338.Count the Number of Ideal Arrays Expand file tree Collapse file tree 1 file changed +22
-32
lines changed Original file line number Diff line number Diff line change 1
- func idealArrays (n int , maxValue int ) int {
2
- mod := int (1e9 ) + 7
3
- m := maxValue
1
+ func idealArrays (n int , maxValue int ) (ans int ) {
2
+ const mod = int (1e9 + 7 )
4
3
c := make ([][]int , n )
5
- f := make ([][]int , m + 1 )
6
- for i := range c {
7
- c [i ] = make ([]int , 16 )
8
- }
9
- for i := range f {
10
- f [i ] = make ([]int , 16 )
11
- for j := range f [i ] {
12
- f [i ][j ] = - 1
13
- }
14
- }
15
- var dfs func (int , int ) int
16
- dfs = func (i , cnt int ) int {
17
- if f [i ][cnt ] != - 1 {
18
- return f [i ][cnt ]
19
- }
20
- res := c [n - 1 ][cnt - 1 ]
21
- if cnt < n {
22
- for k := 2 ; k * i <= m ; k ++ {
23
- res = (res + dfs (k * i , cnt + 1 )) % mod
24
- }
25
- }
26
- f [i ][cnt ] = res
27
- return res
28
- }
29
4
for i := 0 ; i < n ; i ++ {
5
+ c [i ] = make ([]int , 16 )
30
6
for j := 0 ; j <= i && j < 16 ; j ++ {
31
7
if j == 0 {
32
8
c [i ][j ] = 1
@@ -35,9 +11,23 @@ func idealArrays(n int, maxValue int) int {
35
11
}
36
12
}
37
13
}
38
- ans := 0
39
- for i := 1 ; i <= m ; i ++ {
40
- ans = (ans + dfs (i , 1 )) % mod
14
+
15
+ f := make ([][16 ]int , maxValue + 1 )
16
+ for i := 1 ; i <= maxValue ; i ++ {
17
+ f [i ][1 ] = 1
18
+ }
19
+ for j := 1 ; j < 15 ; j ++ {
20
+ for i := 1 ; i <= maxValue ; i ++ {
21
+ for k := 2 ; k * i <= maxValue ; k ++ {
22
+ f [k * i ][j + 1 ] = (f [k * i ][j + 1 ] + f [i ][j ]) % mod
23
+ }
24
+ }
25
+ }
26
+
27
+ for i := 1 ; i <= maxValue ; i ++ {
28
+ for j := 1 ; j < 16 ; j ++ {
29
+ ans = (ans + f [i ][j ]* c [n - 1 ][j - 1 ]) % mod
30
+ }
41
31
}
42
- return ans
43
- }
32
+ return
33
+ }
You can’t perform that action at this time.
0 commit comments