Rotate and Shift
Time limit4sMemory limit1024 MB
Given N cows on a circle and K active positions that rotate their cows and then shift forward by one each minute, output the final cow order after T minutes.
- Level
Medium7 of 10
- Topics
- Math, Array, Simulation
- Solved
- No attempts yet
Problem
To celebrate the start of spring, Farmer John's cows () have invented a new dance. The cows stand in a circle and reorder themselves in a predictable way.
There are positions around the circle, numbered from to , with position following position . Each position holds one cow, and the cows are also numbered from to . Initially, cow is in position . A set of positions is marked as active (). The cows in the active positions move next.
Each minute, two things happen. First, the cows in the active positions rotate: the cow at moves to , the cow at moves to , and so on, and the cow at moves to . All moves happen at the same time, so every active position still holds exactly one cow afterward. Second, the active positions shift: becomes , becomes , and so on. If an active position is , it wraps around to .
Input
The first line contains three integers , , and .
The second line contains integers , the initial set of active positions. Recall that and that these are given in increasing order.
Output
Output the order of the cows after minutes, starting with the cow in position , separated by spaces.