Ant
InterviewTime limit0.15sMemory limit256 MB
Compute the position of an ant moving diagonally and reflecting off the walls of a w by h box after t hours.
- Level
Easy3 of 10
- Topics
- Math
- Solved
- No attempts yet
Problem
A rectangular grid area is wide and tall. Its lower left corner is and its upper right corner is . One ant sits at the point inside this area.
The ant starts moving at a constant speed in the up-right 45 degree direction. After one hour the ant that started at has moved to . When it hits a boundary while moving at that speed, it reflects and keeps the same speed.

The picture above shows the path of an ant that starts at on a grid. That ant is at after 2 hours and at after 8 hours. If the ant had started at on the same grid, it would move to , , , hour by hour.
Compute and print the position of the ant hours after it starts at on a grid of size . Assume the ant never tires and keeps the same speed.
and are natural numbers with . The starting coordinates and are natural numbers with and . The time satisfies .
Input
The first line contains and separated by a space. The second line contains the starting coordinates and separated by a space. The third line contains the time the ant moves.
Output
Print and of the ant position after hours, separated by a space, on one line.