AtCoder Regular Contest 108 - AtCoder C was written with recursive calls and was 21 TLE in PyPy and 16 TLE in raw Python, and with a few minutes left I switched to depth-first search using my own stack, but I bugged it and got a lot of WAs, which I didn't fix in time.
A
def solve(S, P):
i = 1
while i * i <= P:
if P % i == 0:
s = i + P // i
if S == s:
return "Yes"
i += 1
return "No"
B
python
def solve(N, S):
i = 0
state = [0]
ret = N
while i < N:
# debug(i, S[i], state, msg=":i, state")
if state[-1] == 0:
if S[i] == "f":
state.append(1)
elif state[-1] == 1:
if S[i] == "o":
state[-1] = 2
elif S[i] == "f":
state.append(1)
else:
state = [0]
elif state[-1] == 2:
if S[i] == "x":
state.pop()
ret -= 3
elif S[i] == "f":
state.append(1)
else:
state = [0]
i += 1
return ret
C
python
def solve(N, M, edges):
vlabel = [None] * N
def f(cur, parentEdge=None, parent=None):
if parentEdge is None:
vlabel[cur] = 1
else:
if parentEdge != parent:
vlabel[cur] = parentEdge
else:
vlabel[cur] = (parentEdge % N) + 1
for child in edges[cur]:
if vlabel[child]:
continue
c = edges[cur][child]
f(child, c, vlabel[cur])
f(0, None, None)
return vlabel
D
python
def solve(N, S):
if N < 4:
return 1
MOD = 1_000_000_007
S = [x[0] - ord("A") for x in S]
AA, AB, BA, BB = 0, 1, 2, 3
A, B = 0, 1
c = Combination(N + 10, MOD)
if S[AB] == A:
# len(B) = 1
if S[AA] == A:
return 1
if S[AB] == A and S[BA] == A:
# inserted B = length 1
M = N - 2
ret = 0
for i in range(M):
if M - i < i:
break
ret += c.comb(M - i, i)
ret %= MOD
return ret
else:
return pow(2, N - 3, MOD)
else:
if S[BB] == B:
return 1
if S[BA] == B and S[AB] == B:
# inserted B = length 1
M = N - 2
ret = 0
for i in range(M):
if M - i < i:
break
ret += c.comb(M - i, i)
ret %= MOD
return ret
else:
return pow(2, N - 3, MOD)
F
This page is auto-translated from /nishio/ARC108 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.