#GL0000323. Chu kỳ bóng nảy (Bouncing Ball Cycle)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chu kỳ bóng nảy (Bouncing Ball Cycle)
Bouncing Ball Cycle
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
A ball starts at cell of an grid and moves diagonally down and right, one cell per step. On reaching a border, the corresponding direction component is reflected. At a corner both components are reflected.
Compute two quantities: the smallest positive number of steps for the ball to return to , and the number of distinct cells it visits between departure and return, counting the starting cell only once.
Input
One line contains integers , the numbers of rows and columns.
Output
Print two integers: the number of steps until the first return and the number of distinct visited cells.
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- Subtask 3 (50 points): .
Examples
Example 1
Input
3 4
Output
12 6
Explanation
The row period is and the column period is , giving steps. The six visited cells are .
Example 2
Input
2 2
Output
2 2
Explanation
The ball visits after one step and returns to after two, visiting two distinct cells.