← All problemsSign in

Bouncing Ball Cycle

CSESAdditional Problems IIOpen on judge ↗

There is a ball at the top-left corner of an n \times m grid. The rows of the grid are numbered 1,2,\dots,n, and the columns are numbered 1,2,\dots,m. The ball is initially moving diagonally away from the top-left corner. At every step, it moves one cell. Whenever the ball hits the border of the grid, it changes its direction. After how many steps has the ball reached its initial location again?

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start