Game of Throwns
Time limit2sMemory limit512 MB
Track a token around a circle of children through clockwise or counterclockwise throws, undoing the last m throws on command.
- Level
Easy3 of 10
- Topics
- Stack, Simulation, Array
- Solved
- No attempts yet
Problem
Daenerys keeps inventing games for the second grade computer science class she teaches. This week the children stand in a circle and (carefully) throw a petrified dragon egg around.
The children are numbered to clockwise around the circle. Child always starts with the egg. Daenerys calls out one of two things.
- A number , meaning the egg is thrown to the child positions clockwise from the current holder, wrapping around the circle if necessary. If is negative, the throw goes counterclockwise.
- The phrase
undo m, meaning the last throws are undone. Anundocommand never undoes anotherundocommand. It only undoes throws of the first kind.
For example, with children and the four commands 8 -2 3 undo 2, the egg goes from child to child , then from child to child , then from child to child . The undo 2 then sends the egg back from child to child , and back again from child to child . If Daenerys calls out , or any multiple of such as , , or , the child holding the egg throws it straight up and (carefully) catches it again.
Write a program that determines where the egg ends up once every command has been carried out correctly. Do not ask what happens to the children if it is not.
Input
The first line contains the number of children and the number of commands Daenerys calls out . (, )
The second line contains the commands in order. Each command is either an integer () giving how many positions to throw the egg clockwise, or undo m () asking for the last throws to be undone. An undo m counts as one command, so the two kinds together number . Daenerys never has the children undo past the start of the game.
Output
Print the number of the child holding the egg at the end of the game.