-
Notifications
You must be signed in to change notification settings - Fork 15
/
cryptopangrams.py
47 lines (41 loc) · 1.17 KB
/
cryptopangrams.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
43
44
45
46
47
# Copyright (c) 2019 kamyu. All rights reserved.
#
# Google Code Jam 2019 Qualification Round - Problem C. Cryptopangrams
# https://codingcompetitions.withgoogle.com/codejam/round/0000000000051705/000000000008830b
#
# Time: O(LlogN)
# Space: O(1)
#
def gcd(a, b):
while b:
a, b = b, a % b
return a
def cryptopangrams():
N, L = map(int, raw_input().strip().split())
MSG = map(int, raw_input().strip().split())
primes = set()
for i in xrange(L-1):
if MSG[i] == MSG[i+1]:
continue
p = gcd(MSG[i], MSG[i+1])
primes.add(p)
primes.add(MSG[i]//p)
primes.add(MSG[i+1]//p)
if len(primes) == 26:
break
lookup = {}
sorted_primes = sorted(primes)
for i, p in enumerate(sorted_primes):
lookup[p] = chr(ord('A')+i)
for p in sorted_primes:
result = [lookup[p]]
for i in xrange(L):
if MSG[i] % p != 0:
break
p = MSG[i]//p
result.append(lookup[p])
else:
return "".join(result)
return "" # never reach
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, cryptopangrams())