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