Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfa7c61

Browse files
1
1 parent 598bf74 commit cfa7c61

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

‎src/main/kotlin/p27xx/Problem2787.kt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package p27xx
2+
3+
import util.expect
4+
5+
fun main() {
6+
class Solution {
7+
fun numberOfWays(n: Int, x: Int): Int {
8+
val factors = arrayListOf(0)
9+
var t = 1
10+
while (true) {
11+
var num = 1
12+
repeat(x) {
13+
num *= t
14+
}
15+
16+
if (num > n) {
17+
break
18+
}
19+
20+
factors += num
21+
t++
22+
}
23+
24+
val m = 1000000007
25+
26+
val cache = Array(n + 1) { LongArray(factors.size + 1) { -1 } }
27+
fun dfs(num: Int, startNum: Int): Long {
28+
return when {
29+
num == 0 -> {
30+
1
31+
}
32+
33+
cache[num][startNum] >= 0 -> {
34+
cache[num][startNum]
35+
}
36+
37+
else -> {
38+
var result = 0L
39+
40+
for (i in startNum + 1 until factors.size) {
41+
if (num < factors[i]) {
42+
break
43+
}
44+
45+
result += dfs(num - factors[i], i)
46+
result %= m
47+
}
48+
49+
cache[num][startNum] = result
50+
result
51+
}
52+
}
53+
}
54+
55+
return dfs(n, 0).toInt()
56+
}
57+
}
58+
59+
expect {
60+
Solution().numberOfWays(
61+
10, 2
62+
)
63+
}
64+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /