D was difficult, so I solved E first, then went back and solved it and got 5 questions correct.
The problem of counting the number of solutions that exist on a skipped basis.
Tends to be buggy with rounding up and down.
N - 1 to N - A + 1.A==2, it would be.(N - 1) // A.
pythondef solve(N):
ret = N - 1
for i in range(2, N + 1):
ret += (N - 1) // i
return ret
visited is 0, it has not appeared, and if it is non-zero, it is the position at which it first appeared + 1.python
def solve(N, X, M):
visited = [0] * M
a = X
ret = []
for i in range(N):
if visited[a]:
s = sum(ret)
loop_start = visited[a] - 1
loop_end = i
loop_sum = sum(ret[loop_start:loop_end])
loop_length = loop_end - loop_start
loop_count = (N - i) // loop_length
loop_left = (N - i) % loop_length
loop_tail = sum(ret[loop_start:loop_start + loop_left])
return s + loop_count * loop_sum + loop_tail
ret.append(a)
visited[a] = (i + 1)
a = (a * a) % M
return sum(ret)
def main():
from bisect import bisect_left
N, Q = map(int, input().split())
ret = (N - 2) ** 2
xs = [-N]
xvals = [N - 2]
ys = [-N]
yvals = [N - 2]
for _q in range(Q):
q, x = map(int, input().split())
if q == 1:
i = bisect_left(xs, -x)
ret -= xvals[i - 1]
if i == len(xs) and yvals[-1] > x - 2:
ys.append(-xvals[i - 1] - 2)
yvals.append(x - 2)
else:
y = x
i = bisect_left(ys, -y)
ret -= yvals[i - 1]
if i == len(ys) and xvals[-1] > y - 2:
xs.append(-yvals[i - 1] - 2)
xvals.append(y - 2)
print(ret)
This page is auto-translated from /nishio/ABC179 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.