Editorial for COI '09 #2 Kolo
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
We use the sieve of Eratosthenes to find the first prime numbers.
Now we mark the square with and circles with numbers
to
. Since it would be impossible to simulate the entire game within the time limit, we first determine an algorithm that will help us find
's final position. Let's assume that currently
is on position
and the current prime is
:
- If
, then
is in the square so
becomes
.
- Else, if
, then we can determine how many times the person in the square changes place with
:
- If
then
becomes
- Else
becomes
- If
Where is the modulus operator and
is integer division. Now we need to solve the other way so that we may answer the question "If
is on
then who is on
(or
)?"
Let's assume that we know position after the prime
was played.
- If
, then
was at
before prime
.
- Else, if
:
becomes
- If
then
becomes
The time complexity is .
Comments