info

MAPS OS v1.1.4 © 2025

Glory to the Segtree

Glory to the Algorithm.

Ray BouncesGo home

Login to view input

no submission

Login to submit an answer

Problem Leaderboard

Problem Leaderboard

Ray Bounces

You're given a rectangle of integer dimensions n by m. From the bottom-left corner (0, 0), a light ray is fired at a 45° angle toward the top-right. Each time it hits a side, it reflects perfectly (angle of incidence equals angle of reflection). The question: how many side hits (bounces) occur before the ray reaches any rectangle corner?

Input

  • Single line with two integers n and m.
  • n and m are the rectangle's side lengths.
  • The ray starts exactly at the corner (0, 0) heading up-right at 45°.
  • Touching a corner counts as reaching a corner, and the process stops immediately; the final contact is not considered a bounce if it's a corner.
  • We define a bounce as a hit on a side that is not a corner.

Output

  • Single integer: the number of bounces before first reaching any corner.

Example 1 Input

10 15

Example 1 Output

3