-
-
Save neizod/5471797 to your computer and use it in GitHub Desktop.
codejam 1a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sqrt(n): | |
if n == 0: | |
return 0 | |
a, b = divmod(n.bit_length(), 2) | |
x = 2 ** (a + b) | |
while True: | |
y = x + n // x | |
y //= 2 | |
if y >= x: | |
return x | |
x = y | |
def find_it(r, t): | |
pr = 2 * r - 1 | |
rt = sqrt(pr**2 + 8 * t) | |
a1 = -pr + rt | |
a2 = -pr - rt | |
return (a1 if a1 > 0 else a2) // 4 | |
for case in range(int(input())): | |
r, t = [int(n) for n in input().split()] | |
answer = find_it(r, t) | |
print('Case #{}: {}'.format(case+1, answer)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rerere(v, vs, e, E, R, t=0): | |
if not vs: | |
return t+v*e | |
u, us = vs[0], vs[1:] | |
return max(rerere(u, us, e-r+R, E, R, t+v*r) for r in range(R, e+1)) | |
def find_it(E, R, values): | |
v, vs = values[0], values[1:] | |
return rerere(v, vs, E, E, R) | |
for case in range(int(input())): | |
E, R, _ = [int(n) for n in input().split()] | |
if R > E: | |
R = E | |
values = [int(n) for n in input().split()] | |
answer = find_it(E, R, values) | |
print('Case #{}: {}'.format(case+1, answer)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mathapi import factorized | |
from collections import Counter | |
def supcase(kset, N, M): | |
r = Counter() | |
for k in [factorized(k) for k in kset]: | |
r |= Counter(k) | |
while 1 in r: | |
r.pop(1) | |
nset = sorted(p for p, e in r.items() for _ in range(e)) | |
while len(nset) > N: | |
a, b, *rem = nset | |
if a * b > M: | |
nset = sorted([a, b] + rem) | |
else: | |
nset = sorted(rem + [a * b]) | |
while len(nset) != N: | |
nset += [2] | |
return nset | |
for case in range(int(input())): | |
R, N, M, K = [int(n) for n in input().split()] | |
print('Case #{}:'.format(case+1)) | |
for _ in range(R): | |
kset = [int(n) for n in input().split()] | |
answer = supcase(kset, N, M) | |
if not answer: | |
answer = [2] * N | |
print(''.join(map(str, answer))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment