This question evaluates understanding of modular arithmetic, parity tracking, periodic behavior on circular data structures, and the design of efficient preprocessing for repeated queries.
You are given a ring of N positions indexed from 0 to N-1.
i
, there is initially one ball whose color is either white or black.
gate[i]
where
1
means a gate is present and
0
means no gate.
For each query, you are given an integer K and must determine how many balls are white after exactly K steps.
Additional details:
i
will be located at
(i + K) mod N
after
K
steps.
K
moves.
K
may be much larger than
N
, so simulating step by step is too slow.
Design an algorithm that preprocesses the ring and answers each query efficiently, ideally in O(1) time per query after preprocessing.