-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibo_sum.py
More file actions
76 lines (69 loc) · 1.59 KB
/
fibo_sum.py
File metadata and controls
76 lines (69 loc) · 1.59 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def fibo(n):
if n in memo:
return
st = [n]
while st:
curr = st[-1]
flag = False
m = curr // 2
if curr % 2 == 0:
n1 = m + 1
n2 = m - 1
if n1 not in memo:
st.append(n1)
flag = True
if n2 not in memo:
st.append(n2)
flag = True
if flag:
continue
memo[curr] = (memo[n1] ** 2 - memo[n2] ** 2) % 1000000000
st.pop()
else:
n1 = m + 1
n2 = m
if n1 not in memo:
st.append(n1)
flag = True
if n2 not in memo:
st.append(n2)
flag = True
if flag:
continue
memo[curr] = (memo[n1] ** 2 + memo[n2] ** 2) % 1000000000
st.pop()
return
# if n == 0:
# return 0
# elif n == 1 or n == 2:
# return 1
#
# if n in memo:
# return memo[n]
#
# m = n // 2
# if n % 2 == 0:
# result = (fibo(m+1) ** 2 - fibo(m - 1) ** 2) % 1000000000
# # result %= 1000000000
# memo[n] = result
# return result
#
# result = (fibo(m + 1) ** 2 + fibo(m) ** 2) % 1000000000
# # result %= 1000000000
# memo[n] = result
# return result
# N = int(input())
N, M = map(int, input().split())
memo = {
0 : 0,
1 : 1,
2 : 1
}
res = 0
for i in range(N, M, 2):
fibo(i+2)
res += memo[i+2]
if (M - N + 1) % 2 == 1:
fibo(M)
res += memo[M]
print(res % 1000000000)