-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSolution.py.m4
501 lines (388 loc) · 13.6 KB
/
Solution.py.m4
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env python3
FLAGS = set()
# FLAGS.add("DEFAULT_VAL_MODE") #remove comm, to activate default value trigger
DEFAULT_VAL_TRIGGER = lambda result: result is None # noqa: E731
DEFAULT_VAL = "IMPOSSIBLE"
# FLAGS.add("IA_MODE") #remove comm, to activate interactive problem mode
IA_ERROR_CODE = "ERROR"
# The maintained and empty code template can be found at:
# https://github.com/demmerichs/CodeJamTemplate
# #region template code
import functools as ft # noqa: E402,F401
import itertools as it # noqa: E402,F401
import math # noqa: E402,F401
import sys # noqa: E402,F401
import time # noqa: E402,F401
from fractions import Fraction # noqa: E402,F401
from math import factorial as fac # noqa: E402,F401
import numpy as np # noqa: E402,F401
import scipy as sp # noqa: E402,F401
# #region abbr
# #region types
ld = np.float128
v = np.array
# #endregion types
# #region constants: INF, EPS, PI, MOD
INF = 2 ** 64 - 1
EPS = ld(1e-15)
PI = ld("3.14159265358979323846264338328")
MOD = 1000000007
DIRECTIONS = [v([1, 0]), v([0, 1]), v([-1, 0]), v([0, -1])]
DIR_NAMES = "ENWS"
INV_PHI = (np.sqrt(ld(5)) - 1) / 2 # 1 / phi
INV_PHI2 = (3 - np.sqrt(ld(5))) / 2 # 1 / phi^2
# #endregion constants
# #endregion abbr
# #region printerTools
def to_str(val, precision=10):
local_to_str = ft.partial(to_str, precision=precision)
if isinstance(val, (list, tuple, np.ndarray)):
return " ".join(map(local_to_str, val))
if isinstance(val, np.float):
return ("%%.%df" % precision) % val
return str(val)
def cout(*args, end=True, error=False):
kwargs = dict()
precision = 10
if not end:
kwargs["end"] = ""
if error:
kwargs["file"] = sys.stderr
precision = 4
local_to_str = ft.partial(to_str, precision=precision)
s = "".join(map(local_to_str, args))
print(s, **kwargs)
def cerr(*args, **kwargs):
kwargs["error"] = True
cout(*args, **kwargs)
# #endregion printerTools
# #region debugTools
# the following definitions are just for the linter to be satiesfied
# as it cannot detect the m4 macro expansions
def lpdb():
pass
def llog(*args):
pass
def lg(var):
pass
def lassert(*args):
pass
def start_timer():
pass
def stop_timer():
pass
def get_time():
pass
"""
m4 macro expansions
define(`start_timer', `ifdef(`LOCAL',`local_start_timer($@)',`pass')')# define(`start_timer', `ifdef(`LOCAL',`local_start_timer($@)',`pass')')
define(`stop_timer', `ifdef(`LOCAL',`local_stop_timer($@)',`pass')')# define(`stop_timer', `ifdef(`LOCAL',`local_stop_timer($@)',`pass')')
define(`get_time', `ifdef(`LOCAL',`local_get_time($@)',`pass')')# define(`get_time', `ifdef(`LOCAL',`local_get_time($@)',`pass')')
define(`lpdb', `ifdef(`PDB',`local_pdb($@)',`pass')')# define(`lpdb', `ifdef(`PDB',`local_pdb($@)',`pass')')
define(`llog', `ifdef(`LOCAL',`local_log($@)',`pass')')# define(`llog', `ifdef(`LOCAL',`local_log($@)',`pass')')
define(`lg', `ifdef(`LOCAL',`local_log("$@", $@)',`pass')')# define(`lg', `ifdef(`LOCAL',`local_log("$@", $@)',`pass')')
define(`lassert', `ifdef(`LOCAL',`local_assert($@)',`pass')')# define(`lassert', `ifdef(`LOCAL',`local_assert($@)',`pass')')
undefine(`__file__',`__gnu__',`__line__',`__os2__',`__program__',`__unix__',`__windows__',`argn',`array',`array_set',`builtin',`capitalize',`changecom',`changequote',`changeword',`cleardivert',`cond',`copy',`curry',`debugfile',`debugmode',`decr',`define',`define_blind',`defn',`divert',`divnum',`dnl',`downcase',`dquote',`dquote_elt',`dumpdef',`errprint',`esyscmd',`eval',`example',`exch',`fatal_error',`foreach',`foreachq',`forloop',`format',`ifelse',`include',`incr',`index',`indir',`join',`joinall',`len',`m4exit',`m4wrap',`maketemp',`mkstemp',`nargs',`os2',`patsubst',`popdef',`pushdef',`quote',`regexp',`rename',`reverse',`shift',`sinclude',`stack_foreach',`stack_foreach_lifo',`stack_foreach_sep',`stack_foreach_sep_lifo',`substr',`syscmd',`sysval',`traceoff',`traceon',`translit',`undefine',`undivert',`unix',`upcase',`windows')# undefine(`__file__',`__gnu__',`__line__',`__os2__',`__program__',`__unix__',`__windows__',`argn',`array',`array_set',`builtin',`capitalize',`changecom',`changequote',`changeword',`cleardivert',`cond',`copy',`curry',`debugfile',`debugmode',`decr',`define',`define_blind',`defn',`divert',`divnum',`dnl',`downcase',`dquote',`dquote_elt',`dumpdef',`errprint',`esyscmd',`eval',`example',`exch',`fatal_error',`foreach',`foreachq',`forloop',`format',`ifelse',`include',`incr',`index',`indir',`join',`joinall',`len',`m4exit',`m4wrap',`maketemp',`mkstemp',`nargs',`os2',`patsubst',`popdef',`pushdef',`quote',`regexp',`rename',`reverse',`shift',`sinclude',`stack_foreach',`stack_foreach_lifo',`stack_foreach_sep',`stack_foreach_sep_lifo',`substr',`syscmd',`sysval',`traceoff',`traceon',`translit',`undefine',`undivert',`unix',`upcase',`windows')
"""
def local_start_timer():
global start
start = time.time()
def local_stop_timer():
global stop
stop = time.time()
def local_get_time():
global start, stop
return stop - start
def local_pdb():
sys.stdin.readlines()
sys.stdin = open("/dev/tty")
import pdb
pdb.set_trace()
def local_log(*args):
cerr("\t".join(map(str, args)))
def local_assert(*args):
if not args[0]:
llog(args[1:])
lpdb()
# #endregion debugTools
# #region parseTools
# TODO add documentation
def convert_input(val):
try:
return int(val)
except ValueError:
pass
try:
return float(val)
except ValueError:
pass
return val
def cin(n=None):
k = 1 if n is None else n
if len(cin.cached_vals) >= k:
val = cin.cached_vals[:k]
del cin.cached_vals[:k]
if n is None:
return val[0]
return val
else:
v = input()
v = v.strip().split(" ")
cin.cached_vals += list(map(convert_input, v))
return cin(n)
cin.cached_vals = []
# #endregion parseTools
# #region mathTools
# TODO add documentation for operators and euclideanAlgo
# faculty (facll, facmod and facld) IMPORTANT: facll works only for n<=20
# choose (choosell, choosemod and chooseld)
# power on integers (powll and powmod)
# log2 on integers (log2ll)
# ceill(p,q)/floorll (returns ceil(p/q)/floorll(p/q) for integers)
def facmod(n):
if n > 0:
return (n * facmod(n - 1)) % MOD
return 1
def choosell(n, k):
if k > n:
return 0
if n - k < k:
return choosell(n, n - k)
result = 1
for i in range(k):
result *= n - i
result //= i + 1
return result
def choosemod(n, k):
if k > n:
return 0
if len(choosemod.memomrize) > n:
if len(choosemod.memorize[n]) > k:
return choosemod.memorize[n][k]
else:
choosemod(n, k - 1)
choosemod.memorize[n].append(
(choosemod(n - 1, k - 1) + choosemod(n - 1, k)) % MOD
)
return choosemod.memorize[n][k]
else:
while len(choosemod.memorize) <= n:
choosemod.memorize.append([1])
return choosemod(n, k)
choosemod.memorize = []
def chooseld(n, k):
if k > n:
return 0
if n - k < k:
return choosell(n, n - k)
result = 1
for i in range(k):
result *= n - i
result /= i + 1
return result
def powmod(base, exp):
if exp == 0:
return 1
elif exp & 1:
return (powmod(base, exp - 1) * base) % MOD
else:
t = powmod(base, exp // 2)
return (t * t) % MOD
def log2ll(n):
assert n > 0
if n == 1:
return 0
return 1 + log2ll(n >> 1)
# Implementation returns the values corresponding values for a, b
# a_in * a_out + b_in * b_out = gcd(a, b) (> 0, trivial solution =0 excluded)
def euclideanAlgo(a, b):
# only process, a,b >= 0, a<=b; transform all other cases to this
if a < 0:
a = -a
a, b = euclideanAlgo(a, b)
a = -a
return a, b
if b < 0:
b = -b
a, b = euclideanAlgo(a, b)
b = -b
return a, b
if a > b:
b, a = euclideanAlgo(b, a)
return a, b
if a == 0:
lassert(b >= 0, "euclideanAlgo: Logic error")
lassert(b > 0, "euclideanAlgo: Both numbers are zero")
b = 1
return a, b
s = b // a
r = b - s * a
r, a = euclideanAlgo(r, a)
a = a - s * r
b = r
return a, b
def gcd(a, b):
c, d = euclideanAlgo(a, b)
return a * c + b * d
# chinese remainder theorem in application
def crt(remainders, moduli):
lassert(
len(remainders) == len(moduli),
"Chinese remainder theorem: remainders and moduli must be of same size!",
)
idxs = np.argsort(moduli)[::-1]
m = 1
r = 0
for idx in idxs:
# solve a*m1 + b*m2 == 1
cur_modulo = moduli[idx]
cur_remainder = remainders[idx]
a, b = euclideanAlgo(m, cur_modulo)
# make sure, that m1 and m2 are indeed coprime
lassert(
a * m + b * cur_modulo == 1,
"Chinese remainder theorem: euclidean algorithm delivered unexpected result! Are your factors coprime?",
)
# crt: find x == r1 (m1) and x == r2 (m2)
# solved by x = r1 + (r2 - r1) * a * m1 == r2 + (r1 - r2) * b * m2
# proof (for first expression): x == r1 (m1) trivial, x == r1 + (r2 - r1) * (a * m1 (m2)) == r1 + (r2 - r1) * 1 == r2 (m2)
r = r + (cur_remainder - r) * a * m
m *= cur_modulo
r = (r % m + m) % m
return r
# #endregion mathTools
# #region algoTools
def gss(
f,
a,
b,
*,
upper=False,
tol=1e-5,
c=None,
d=None,
fa=None,
fb=None,
fc=None,
fd=None
):
"""Golden section search, recursive.
Given a strictly unimodal function f with an interval local minimum [c,d] in
the interval [a,b], gss returns (depending on upper) a value pair x, y=f(x)
with x being within tolerance of either the upper interval bound d or the lower c.
"""
(a, b) = (min(a, b), max(a, b))
fa = f(a) if fa is None else fa
fb = f(b) if fb is None else fb
h = b - a
t = type(h)
c = t(a + INV_PHI2 * h) if c is None else c
if c == a:
c += tol
fc = None
d = t(a + INV_PHI * h) if d is None else d
if d == b:
d -= tol
fd = None
fc = f(c) if fc is None else fc
fd = f(d) if fd is None else fd
if h < tol:
return [(a, fa), (b, fb)][upper]
if d < c:
if fc < fd:
return c, fc
return d, fd
m = min(fa, fb, fc, fd)
if fa == m and fc > m and fd > m:
return a, fa
if fb == m and fc > m and fd > m:
return b, fb
if fc < fd or (not upper and fc == fd):
return gss(f, a, d, upper=upper, tol=tol, fa=fa, fb=fd, d=c, fd=fc)
return gss(f, c, b, upper=upper, tol=tol, fa=fc, fb=fb, c=d, fc=fd)
# #endregion algoTools
# #region main
if "IA_MODE" in FLAGS:
def get_in():
in_value = cin()
if in_value == IA_ERROR_CODE:
quit()
llog("reading value:\t", in_value)
return in_value
def out(t):
llog("sending output:\t", t)
cout(t)
result = ""
def main():
nbr_tests = cin()
init()
for test_id in range(1, 1 + nbr_tests):
llog()
llog()
llog()
llog()
llog("`################################################'")
llog("`################################################'")
llog("`################################################'")
llog("`################", test_id, "################'")
llog("`################################################'")
llog("`################################################'")
llog("`################################################'")
llog()
llog("============ reading input ============")
start_timer()
readInput()
stop_timer()
llog("-----------", get_time(), "secs -----------")
llog()
llog("============ doing computation ============")
start_timer()
calcFunction()
stop_timer()
llog("-----------", get_time(), "secs -----------")
llog()
if "IA_MODE" not in FLAGS:
# write output
cout("Case #%d: " % test_id, end=False)
if "DEFAULT_VAL_MODE" not in FLAGS:
cout(result)
if "DEFAULT_VAL_MODE" in FLAGS:
if DEFAULT_VAL_TRIGGER(result):
llog("default val triggered")
cout(DEFAULT_VAL)
else:
llog("default val NOT triggered")
cout(result)
# #endregion main
# #endregion template code
def init():
global result
def readInput():
global result, N, L, numbers
N, L = cin(2)
numbers = cin(L)
# write to result
def calcFunction():
global result, N, L, numbers
i = 0
while numbers[i] == numbers[i + 1]:
i += 1
primeip1 = math.gcd(numbers[i], numbers[i + 1])
primesbeforeip1 = [primeip1]
for k in range(i, -1, -1):
primesbeforeip1.append(numbers[k] // primesbeforeip1[-1])
primesbeforeip1 = primesbeforeip1[1:]
primesbeforeip1 = primesbeforeip1[::-1]
primesafterip1 = [primeip1]
for k in range(i + 2, L + 1):
primesafterip1.append(numbers[k - 1] // primesafterip1[-1])
primesafterip1 = primesafterip1[1:]
primes = primesbeforeip1 + [primeip1] + primesafterip1
sorted_primes = sorted(list(set(primes)))
assert len(sorted_primes) == 26
result = ""
for prime in primes:
result += chr(sorted_primes.index(prime) + ord("A"))
assert len(set(result)) == 26
if __name__ == "__main__":
main()