Laser Pool
Time limit5sMemory limit256 MB
A ball bounces elastically around a grid of lit horizontal and vertical laser beams; count how many distinct lit beams it touches during t time units, including the start.
- Level
Hard8 of 10
- Topics
- Math, Simulation, Number theory, Implementation
- Solved
- No attempts yet
Problem
The people of Byteotia love a game called laser pool. The table is an rectangle. Its rails are wide and hold pairs of laser transmitters. When every transmitter is switched on, the table is covered by horizontal and vertical laser beams, arranged so that the -th horizontal beam (for ) meets the -th vertical beam (for ) at the point . Each transmitter can be turned on or off independently.
The game uses a ball of diameter . Whenever the ball touches at least one lit laser beam, a single hit signal is shown; touching several beams at the very same instant still counts as one signal.
At the start the ball is centered at . It is struck so that its initial velocity vector is , and it then rolls without friction for units of time. Every collision with a rail is perfectly elastic. How many times is the hit signal shown, counting the initial moment of the ball's motion as well?
Input
The first line contains two integers and (), the dimensions of the table.
The second line is a string of characters, each either or . Its -th character is the state of the -th horizontal transmitter, where means off and means on.
The third line is a string of characters describing the vertical transmitters in the same way.
The fourth line contains an integer (), the number of queries. Each of the next lines contains five integers , , , , (, , , ): the starting position of the ball, its velocity, and how long it rolls.
Output
Print exactly lines. The -th line must contain one integer: the number of times the hit signal is shown for the -th query.
Hint
