50081. Robot Simulation

I'm a slow walker, but I never walk backwards.

Write a program to simulate a robot movement on a grid.

Task Description

We have an $N$ by $M$ grid $G$ of squares with a robot in it. The horizontal index is from $0$ to $N-1$, and the vertical index is from $0$ to $M-1$. The robot can either move right, left, up and down within the grid. The robot moves according to a sequence of instructions, as positive integers between $1$ and $1000$. The robot moves as follows.

  • If the instruction is $5n$, it is an illegal instruction, the robot will not move.
  • If the instruction is $5n+1$, the robot moves right by the value of the instruction.
  • If the instruction is $5n+2$, the robot moves left by the value of the instruction.
  • If the instruction is $5n+3$, the robot moves up by the value of the instruction.
  • If the instruction is $5n+4$, the robot moves down by the value of the instruction.

The Coordinate system of robot is difined as follows:

p50081. Coordinate

Note that if an instruction intends to move the robot out of the grid, the instruction is illegal, and the robot will not move.

Write a program to simulate a robot movement on a grid, and output the location of the robot for every instruction, except an illegal one. The program simulates the robot, which is initially located at $(0,0)$, and the movement according to the input instructions until the end of file. It is guaranteed that there is at least one instruction.

Subtask

  • 50 points: there are no illegal instructions.
  • 50 points: there are illegal instructions.

Input Format

The input contains only one test case. The first line contains $N$ and $M$, representing the width and height of grids. The second line contains the instruction for the robot, and the number of instructions is at least one.

Output Format

Print two lines for the initial location and each instruction except an illegal instruction. The first line contains one integer, representing the horizontal index of the robot, and the second line contains one integer, representing the vertical index of the robot.

Note

Do not use array for this problem. You can solve this problem with very little memory. We will make sure that you will not have enough memory, and see the "memory limit exceeded" error, if you use array.

Sample Input 1

10 20
1 3 6 7 8

Sample Output 1

0
0
1
0
1
3
7
3
0
3
0
11

Sample Input 2

10 20
1 2 3 4 5 6 7 8

Sample Output 2

0
0
1
0
1
3
7
3
0
3
0
11

Discussion