-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path36.py
More file actions
44 lines (32 loc) · 1 KB
/
36.py
File metadata and controls
44 lines (32 loc) · 1 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
import sys
from queue import Queue
def FILE_IO():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def get_input():
key, lock = map(int, input().split())
input()
other_keys = list(map(int, input().split()))
return key, lock, other_keys
def bfs(key, lock, other_keys):
q = Queue()
q.put(key)
visited = dict()
visited[key] = 0
while not q.empty():
key_comb = q.get()
key_comb_visited = visited.get(key_comb)
for ok in other_keys:
new_key_comb = (key_comb * ok) % 100000
if visited.get(new_key_comb) is None:
visited[new_key_comb] = key_comb_visited + 1
q.put(new_key_comb)
if new_key_comb == lock:
return visited.get(new_key_comb)
return -1
def solve():
key, lock, other_keys = get_input()
res = bfs(key, lock, other_keys)
print(res)
# FILE_IO()
solve()