-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11778.py
42 lines (30 loc) · 834 Bytes
/
11778.py
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
from sys import stdin
input = stdin.readline
def solve():
mod = 1_000_000_007
def mul_mat(mat1, mat2):
ret = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
ret[i][j] += mat1[i][k] * mat2[k][j]
ret[i][j] %= mod
return ret
def fibonacci(num):
if num <= 2: return 1
ans = [[1, 0], [0, 1]]
mat = [[1, 1], [1, 0]]
while num:
if num % 2:
ans = mul_mat(ans, mat)
mat = mul_mat(mat, mat)
num //= 2
return ans[0][1]
def gcd(x, y):
while y:
x, y = y, x % y
return x
N, M = map(int, input().split())
return fibonacci(gcd(N, M))
if __name__ == '__main__':
print(solve())