Billiard
시간 제한1초메모리 제한2048 MB
가로 n, 세로 m인 당구대의 한 모서리에서 45도로 출발한 공이 처음 위치로 되돌아오는 데 걸리는 단위 이동 횟수를 구한다.
문제
There is a table with length and width .
A billiard ball begins to move from one corner with an angle of degrees.
When will the ball bounce back to where it starts?
Formally, you are given and , and you need to calculate the return value of the following function.
int64_t check(int n, int m) {
int x = 0, y = 0;
int dx = 1, dy = 1;
int64_t t = 0;
while (1) {
if (x + dx < 0) dx *= -1;
if (x + dx > n) dx *= -1;
if (y + dy < 0) dy *= -1;
if (y + dy > m) dy *= -1;
x += dx;
y += dy;
++t;
if (x == 0 && y == 0) break;
}
return t;
}
입력
The first line contains an integer , the number of test cases (). The test cases follow.
Each test case is described by a single line containing two integers and ().
출력
For each test case, output a line containing one integer: the answer to the problem.