Advertisement
AquaBlitz11

Cryptopangrams (Test set 2)

Apr 7th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. from math import gcd
  2.  
  3. T = int(input())
  4. for t in range(1, T+1):
  5.     N, L = map(int, input().split())
  6.     arr = list(map(int, input().split()))
  7.     P = [0]*(L+1)
  8.  
  9.     # find starting point
  10.     fnd = 0
  11.     for i in range(L-1): # loop from 0 to L-2
  12.         g = gcd(arr[i], arr[i+1])
  13.         if g != arr[i]:
  14.             fnd = i
  15.             P[i] = arr[i]//g
  16.             break
  17.     # go left and right
  18.     for i in range(fnd+1, L+1): # loop from fnd+1 to L
  19.         P[i] = arr[i-1]//P[i-1]
  20.     for i in range(fnd-1, -1, -1): # loop from fnd-1 to 0 (backward)
  21.         P[i] = arr[i]//P[i+1]
  22.     # build dictionary
  23.     LP = dict()
  24.     for k, v in enumerate(sorted(set(P))):
  25.         LP[v] = chr(k+ord('A'))
  26.     for i in range(L+1):
  27.         P[i] = LP[P[i]]
  28.     print("Case #" + str(t) + ": " + "".join(P))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement