-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteps.py
More file actions
57 lines (45 loc) · 1.02 KB
/
steps.py
File metadata and controls
57 lines (45 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from functools import cache
M = 10**9 + 7
_fact = [1] * 100
def build_fact():
global _fact
_fact = [1] * 100
for i in range(1, 100):
_fact[i] = _fact[i - 1] * i
def _get_fact(value: int):
global _fact
for i in range(len(_fact) - 1, -1, -1):
if _fact[i] <= value:
return i
return -1
@cache
def _build_steps(n: int, last: int):
if n == 0:
return 0
if n > 1 and last == 2:
return 0
if n == 1:
return last > 1
count = 0
i = _get_fact(n)
n0 = n - i
while last - i >= 1 and n0 > 0:
count += _build_steps(n0, last=i)
n0 -= 1
i += 1
if n0 == 0 and last - i >= 1:
count += 1
return count
def build_steps(n: int):
if n == 1:
return 1
if n == 2:
return 0
_count = 1
build_fact()
for i in range(_get_fact(value=n), n):
_count += _build_steps(n - i, i)
return _count
if __name__ == "__main__":
n = int(input().rstrip())
print(build_steps(n))