I was aware of the symptoms of being sick before it started, but I'm a wreck. 3 completions in and the rating has dropped for the first time.
def solve(S, T):
buf = []
for i in range(len(S) - len(T) + 1):
diff = 0
for j in range(len(T)):
if S[i + j] != T[j]:
diff += 1
buf.append(diff)
return min(buf)
len(S) - len(T) + 1 run-time errorpython
def main():
global parent, rank
# parse input
N, M = map(int, input().split())
parent = [-1] * N
rank = [0] * N
for q in range(M):
a, b = map(int, input().split())
unite(a - 1, b - 1)
xs = list(find_root(x) for x in range(N))
# debug(": xs", xs)
# print(len(set(xs))) # WRONG!
from collections import Counter
print(max(Counter(xs).values()))
E - Coprime
- Eratosthenes' sieve to determine the prime number from the smallest, while dividing N numbers by that prime number.
- No need to retain the result of prime factorization, since it is obtained by paying attention to each prime factor for the determination of the problem
- When there are d numbers out of N that have some prime factor p,
- Cannot be PAIRWISE if d is greater than or equal to 2
- If d is N, it is fixed to NOT COPRIME.
- Unfortunately, TLE
- Eleven of the ones in between were the right answers.
python
def solve(N, AS):
is_pairwise = True
maxAS = max(AS)
sieved = [False] * (maxAS + 10)
for p in range(2, maxAS + 1):
if sieved[p]:
continue
# p is prime
x = p + p
while x <= maxAS:
sieved[x] = True
x += p
sum_div = 0
for i, a in enumerate(AS):
dividable = 0
while a % p == 0:
a //= p
dividable = 1
AS[i] = a
sum_div += dividable
if sum_div >= 2:
is_pairwise = False
if sum_div == N:
return "not coprime"
if is_pairwise:
return "pairwise coprime"
return "setwise coprime"
python
def solve(N, AS):
maxAS = max(AS)
eratree = [0] * (maxAS + 10)
for p in range(2, maxAS + 1):
if eratree[p]:
continue
# p is prime
eratree[p] = p
x = p * p
while x <= maxAS:
if not eratree[x]:
eratree[x] = p
x += p
count = defaultdict(int)
for a in AS:
factors = []
while a > 1:
d = eratree[a]
factors.append(d)
a //= d
for f in set(factors):
count[f] += 1
if any(x == N for x in count.values()):
return "not coprime"
if any(x >= 2 for x in count.values()):
return "setwise coprime"
return "pairwise coprime"
Counted and compared the number of divisions.
This page is auto-translated from /nishio/ABC177 using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.