Mirror Reflection - Problem
Mirror Reflection is a fascinating geometry problem that combines physics with mathematics.

Imagine a special square room where all four walls are perfect mirrors. The room has walls of length p units. In three of the four corners (excluding the southwest corner), there are laser receptors numbered 0, 1, and 2:

Receptor 0: Located at the southeast corner
Receptor 1: Located at the northeast corner
Receptor 2: Located at the northwest corner

A laser beam starts from the southwest corner and travels at a 45-degree angle, first hitting the east wall at a distance q from receptor 0. The beam will bounce off the mirrored walls following the law of reflection (angle of incidence equals angle of reflection) until it eventually hits one of the three receptors.

Goal: Given integers p (wall length) and q (initial hit distance), determine which receptor the laser beam will hit first.

Note: The test cases guarantee that the beam will eventually hit a receptor.

Input & Output

example_1.py — Basic Case
$ Input: p = 2, q = 1
Output: 2
💡 Note: The laser starts at (0,0) and first hits the east wall at (2,1). After bouncing around the mirrored walls, it eventually reaches receptor 2 at the northwest corner. Using the mathematical approach: LCM(2,1) = 2, so m = 2/1 = 2, n = 2/2 = 1. Since m is even and n is odd, but this doesn't match our rules directly - we need m odd, n even for receptor 2.
example_2.py — Another Case
$ Input: p = 3, q = 1
Output: 1
💡 Note: With p=3 and q=1, the laser follows a different path. LCM(3,1) = 3, so m = 3/1 = 3, n = 3/3 = 1. Since both m and n are odd, the laser hits receptor 1 (northeast corner).
example_3.py — Symmetric Case
$ Input: p = 4, q = 2
Output: 0
💡 Note: This creates a more complex bouncing pattern. LCM(4,2) = 4, so m = 4/2 = 2, n = 4/4 = 1. Since m is even and n is odd, the laser hits receptor 0 (southeast corner).

Constraints

  • 1 ≤ p ≤ 106
  • 1 ≤ q ≤ 106
  • The laser will always eventually hit a receptor
  • All inputs guarantee a valid solution exists

Visualization

Tap to expand
Mirror Reflection: Room Unfolding Technique012STARTOriginal RoomComplex bouncingUnfolded GridStraight line path!Mathematical Solution:1. Calculate LCM(p, q)2. m = LCM ÷ q, n = LCM ÷ p3. Check parity of m, n:• m even, n odd → receptor 0• m odd, n odd → receptor 1• m odd, n even → receptor 2Why This WorksEach reflection flips the room's orientation in the unfolded gridThe laser travels in a straight line to point (m×p, n×q)Parity of m,n tells us which corner we hit in the final room copy💡 Key InsightInstead of simulating O(p+q) bounces with floating-point arithmetic,we solve it mathematically in O(log min(p,q)) time using integer operations!
Understanding the Visualization
1
Original Problem
Laser bounces around mirrored walls in complex patterns
2
Unfold the Room
Create a grid of room copies, alternating orientations based on reflections
3
Straight Line Path
The laser now travels in a straight line from origin to destination
4
Mathematical Solution
Use LCM to find the exact intersection point and determine the receptor
Key Takeaway
🎯 Key Insight: Mathematical unfolding transforms a complex physics simulation into an elegant number theory problem using LCM and parity checks.
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
42.0K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen