[AtCoder] E - Putting Candies

Review->Improve發表於2024-08-16

Problem Link

If we pick A[i] the 2nd time, it means we have a cycle.

Proof:

1st time we pick A[i], the sum before adding A[i] is x; 2nd time we pick A[i], the sum before adding A[i] is y; For this to happen x % N == y % N must hold. Otherwise we would not arrive at index i again.

x -> x + A[i]

y -> y + A[i]

(x + A[i]) % N == (y + A[i]) % N. This means we'll repeat the same set of picks in order indefinitely until we run out of operations.

By pigeon hole principle, we'll have a cycle after at most N operations.

The final answer can be constructed accordingly using the above information.

相關文章