Skip to content

Commit 94e4a99

Browse files
committed
Improved task
1 parent c0f06b1 commit 94e4a99

File tree

1 file changed

+27
-25
lines changed
  • src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences

1 file changed

+27
-25
lines changed

src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ package g3501_3600.s3539_find_sum_of_array_product_of_magical_sequences
44
// #2025_05_06_Time_60_ms_(100.00%)_Space_48.98_MB_(100.00%)
55

66
class Solution {
7-
private val mod = 1000000007
8-
private val c: Array<IntArray> = precomputeBinom(31)
9-
private val p: IntArray = precomputePop(31)
10-
117
fun magicalSum(m: Int, k: Int, nums: IntArray): Int {
128
val n = nums.size
139
val pow = Array<LongArray>(n) { LongArray(m + 1) }
1410
for (j in 0..<n) {
1511
pow[j][0] = 1L
1612
for (c in 1..m) {
17-
pow[j][c] = pow[j][c - 1] * nums[j] % mod
13+
pow[j][c] = pow[j][c - 1] * nums[j] % MOD
1814
}
1915
}
2016
var dp = Array<Array<LongArray>>(m + 1) { Array<LongArray>(k + 1) { LongArray(m + 1) } }
@@ -42,12 +38,12 @@ class Solution {
4238
(
4339
next[t + cc][o + (total and 1)][total ushr 1] +
4440
dp[t][o][c] *
45-
this@Solution.c[m - t][cc] %
46-
mod
41+
C[m - t][cc] %
42+
MOD
4743
* pow[i][cc] %
48-
mod
44+
MOD
4945
) %
50-
mod
46+
MOD
5147
)
5248
}
5349
}
@@ -60,31 +56,37 @@ class Solution {
6056
var res: Long = 0
6157
for (o in 0..k) {
6258
for (c in 0..m) {
63-
if (o + p[c] == k) {
64-
res = (res + dp[m][o][c]) % mod
59+
if (o + P[c] == k) {
60+
res = (res + dp[m][o][c]) % MOD
6561
}
6662
}
6763
}
6864
return res.toInt()
6965
}
7066

71-
private fun precomputeBinom(max: Int): Array<IntArray> {
72-
val res = Array<IntArray>(max) { IntArray(max) }
73-
for (i in 0..<max) {
74-
res[i][i] = 1
75-
res[i][0] = res[i][i]
76-
for (j in 1..<i) {
77-
res[i][j] = (res[i - 1][j - 1] + res[i - 1][j]) % mod
67+
companion object {
68+
private const val MOD = 1000000007
69+
private val C: Array<IntArray> = precomputeBinom(31)
70+
private val P: IntArray = precomputePop(31)
71+
72+
private fun precomputeBinom(max: Int): Array<IntArray> {
73+
val res = Array<IntArray>(max) { IntArray(max) }
74+
for (i in 0..<max) {
75+
res[i][i] = 1
76+
res[i][0] = res[i][i]
77+
for (j in 1..<i) {
78+
res[i][j] = (res[i - 1][j - 1] + res[i - 1][j]) % MOD
79+
}
7880
}
81+
return res
7982
}
80-
return res
81-
}
8283

83-
private fun precomputePop(max: Int): IntArray {
84-
val res = IntArray(max)
85-
for (i in 1..<max) {
86-
res[i] = res[i shr 1] + (i and 1)
84+
private fun precomputePop(max: Int): IntArray {
85+
val res = IntArray(max)
86+
for (i in 1..<max) {
87+
res[i] = res[i shr 1] + (i and 1)
88+
}
89+
return res
8790
}
88-
return res
8991
}
9092
}

0 commit comments

Comments
 (0)