This question evaluates understanding of algorithmic state representation, modular arithmetic, and efficient update/query design by requiring a Kac ring implementation that supports constant-time k-step advancement and O(1) aggregate color reporting.
You are implementing a Kac ring dynamical system.
0, 1, ..., N-1
.
At each step:
i
moves to position
(i + 1) mod N
.
Implement a class/data structure with the following interface:
KacRing(N, marked)
N
: number of positions.
marked
: a
sorted
list of
distinct
marked indices
0 <= y1 < ... < ym < N
.
step()
kstep(k)
k
steps).
color()
where `W` is the current number of white balls and `B` is the current number of black balls.
1 <= N
.
0 <= len(marked) <= N
.
k
can be very large (e.g., up to
10^18
), so
kstep
cannot simulate step-by-step.