Billiard

시간 제한1초메모리 제한2048 MB

요약
가로 n, 세로 m인 당구대의 한 모서리에서 45도로 출발한 공이 처음 위치로 되돌아오는 데 걸리는 단위 이동 횟수를 구한다.
난이도

보통10점 중 5점

유형
수학, 정수론, 시뮬레이션
정답자
아직 제출이 없습니다

문제

There is a table with length nn and width mm.

A billiard ball begins to move from one corner with an angle of 4545 degrees.

When will the ball bounce back to where it starts?

Formally, you are given nn and mm, 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 tt, the number of test cases (1≤t≤1051 \le t \le 10^5). The test cases follow.

Each test case is described by a single line containing two integers nn and mm (2≤n,m≤1092 \le n, m \le 10^9).

출력

For each test case, output a line containing one integer: the answer to the problem.

예제1

  1. 예제 1

    입력
    5
    2 2
    2 3
    2 4
    2 5
    2 6
    
    예상 출력
    4
    12
    8
    20
    12