The Bovine Fire Drill
Time limit1sMemory limit128 MB
Simulate cows on a circular ring of N chairs, each moving clockwise by its own number, and report the cow that ends the drill.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
cows () sit in a perfect circle around a campfire. For convenience the cows are numbered through , and the chairs are likewise numbered through ; initially cow sits in chair .
After one of Farmer John's stories ends, he suggests a Bovine Fire Drill.
In a Bovine Fire Drill, only one cow moves at a time. When it is cow 's turn, she stands up and walks clockwise to the chair that is positions away from the chair she is sitting in. (For example, if it is cow 's turn, she moves three chairs clockwise from where she is.)
When cow reaches her new chair, she taps the cow sitting there on the shoulder. That cow stands up to make room, cow sits down, and the cow who just stood up takes the next turn, moving in the same way.
The process continues until one of the following happens:
- a cow arrives at the empty chair, or
- a cow is asked to move for a second time; that is, a cow who has already moved once is tapped again.
Because cow always moves first, the empty chair is always the chair where cow originally sat.
Thanks to the properties of whole numbers the drill usually ends early, so it is rare for every cow to take part. The last cow to move — whether she ends the drill by sitting in the empty chair or by tapping a cow that has already moved — receives a special treat of tender spring grass.
Help Farmer John figure out in advance which cow will receive the treat.
For example, suppose five cows are sitting around the campfire. Below, * marks the empty chair.
2 - 3
( )
1 - 5 - 4
First, cow walks one chair and taps cow , who stands up.
2
1 - 3
( )
* - 5 - 4
Cow walks two chairs and taps cow , who begins her journey.
1 - 3
( )
* - 5 - 2
4
Cow walks four chairs and taps cow .
3
1 - 4
( )
* - 5 - 2
Finally, cow walks three chairs to the empty chair , which ends the drill.
1 - 4
( )
3 - 5 - 2
Cow receives tender spring grass as the other cows clap and cheer.
Input
The first line contains the integer .
Output
Print the number of the cow who ends the drill.