Eenie Meenie Miney Moe
Time limit2sMemory limit512 MB
Simulate elimination in a circle of N heifers using a repeating sequence of up to L step sizes, and report the last heifer left.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Queue
- Solved
- No attempts yet
Problem
Bessie hums a counting rhyme when she picks a calf to play with. Today she gives a hunk of hay made from tender spring grass to one of N heifers numbered 1 to N. The heifers stand in a circle in numeric order.
The order of elimination comes from an integer sequence of length L. Every element of the sequence is between 1 and N. Counting starts at heifer 1, and the heifer the count starts on counts as 1. The heifer reached after counting the first value of the sequence is eliminated. Counting then restarts from the heifer after the one just eliminated, which counts as 1, and the heifer reached after the second value of the sequence is eliminated. Both the circle and the sequence wrap around. When the sequence is used up it starts over from the beginning, and heifers already eliminated are skipped instead of counted. Repeat until one heifer remains and report its number.
For example, take 8 heifers and the elimination sequence 5, 3. Counting 5 from heifer 1 gives 1, 2, 3, 4, 5, so heifer 5 goes out. Counting 3 from heifer 6 gives 6, 7, 8, so heifer 8 goes out. The sequence is used up, so it starts over. Counting 5 from heifer 1 gives 1, 2, 3, 4, 6 because heifer 5 is gone, so heifer 6 goes out. Counting 3 from heifer 7 gives 7, 1, 2, so heifer 2 goes out. Heifers 3, 1 and 4 leave next, and heifer 7 is left standing.

Input
The input holds one or more data sets and runs to the end of the file. Each data set uses two lines.
- Line 1: the number of heifers N and the length of the sequence L, separated by a space (, ).
- Line 2: L space-separated integers that form the elimination sequence. Every value is between 1 and N.
Read data sets until the end of the file.
Output
For each data set, print the number of the heifer left standing on its own line.