#P6350. [PA 2011] Laser Pool
[PA 2011] Laser Pool
Problem Description
In an grid (with the bottom-left corner at and the top-right corner at ), there is a small ball (which can be treated as a point). Some rows and some columns contain laser beams. There are queries. Each query gives the ball’s initial position, direction, speed, and travel time (see the input format for details). You need to find how many times the ball touches a laser beam. If it touches an intersection point of a row-beam and a column-beam, it is counted only once.
When the ball touches the boundary, it reflects. That is, suppose the current change per second is that the coordinate increases by and the coordinate increases by . If it hits the top or bottom boundary, then becomes . If it hits the left or right boundary, then becomes . If this is still unclear, refer to the picture in the sample explanation.
Input Format
The first line contains two integers , representing the number of rows and columns of the grid.
The next line is a binary string of length . If the -th character is , then row has a laser beam; otherwise, it does not.
The next line is a binary string of length . If the -th character is , then column has a laser beam; otherwise, it does not.
The next line contains an integer , representing the number of queries.
The following lines each contain five integers , meaning that initially the ball is at . Each second, increases by and increases by , and it moves for a total of seconds. It is guaranteed that are both or .
Output Format
For each query, output one number per line, representing the number of times the ball touches a laser beam.
4 6
1010
010110
1
5 2 1 1 8
6
Hint

Constraints: , , . It is guaranteed that the ball’s initial position is inside the grid and not on the boundary, and are both or .
Translated by ChatGPT 5