Codeforces Round 1091 (Div. 2) and CodeCraft 26
C
Grid Covering
Codeforces
1300
原题
← BD →
翻译中…

Prakul has been working hard setting problems for Codecraft. When he's deep in thought, he likes jumping around his room in weird but specific ways. After some time, he wonders if jumping in this manner can cover all the tiles of his room.

翻译中…

Prakul's room can be considered as a grid $A$ consisting of $n$ rows and $m$ columns. He starts walking from $A_{1,1}$. If he is currently at $A_{i,j}$ he can do either of the following moves:

翻译中…
  • Jump $b$ steps right to $A_{\,i,\;((j+b-1)\bmod m)+1}$, or
  • Jump $a$ steps down to $A_{\,((i+a-1)\bmod n)+1,\;j}$.
翻译中…

A special restriction is that he can start with either move, but must alternate between them.

翻译中…

Note that his room is quite special as it wraps around itself. Moving one step right from the $m$-th column places him in the $1$-st column. Similarly, moving one step down from the $n$-th row places him in the $1$-st row.

翻译中…

Since he still has to work on problemsetting, he needs your help. Determine if he can visit all tiles in $A$ in a finite number of jumps.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The only line of each test case contains four integers $n, m, a, b$ ($1 \leq n, m, a, b\leq 10^{9}$).

翻译中…

Output

翻译中…

For each test case, print "YES" if Prakul can cover all the tiles of his room, and "NO" otherwise.

翻译中…

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

翻译中…

Note

翻译中…

In the second test case: $n=2$, $m=2$, $a=1$, $b=1$. One possible way to cover the grid is if Prakul starts by moving down:

翻译中…
  • Start at $(1, 1)$.
  • Jump down $a=1$ tile: $(1, 1) \to (2, 1)$.
  • Jump right $b=1$ tile: $(2, 1) \to (2, 2)$.
  • Jump down $a=1$ tile: $(2, 2) \to (1, 2)$.
  • Jump right $b=1$ tile: $(1, 2) \to (1, 1)$.
翻译中…

The sequence of visited tiles is $\{(1, 1), (2, 1), (2, 2), (1, 2)\}$. In this case, all $2 \cdot 2 = 4$ tiles are covered, so the answer would be "YES".

翻译中…

Notice that Prakul wraps around the grid in the last two moves.

翻译中…

In the third test case: $n=4$, $m=2$, $a=2$, $b=1$. If Prakul starts by moving down, the moves look like:

翻译中…
  • Start at $(1, 1)$.
  • Jump down $a=2$ tiles: $(1, 1) \to (3, 1)$.
  • Jump right $b=1$ tile: $(3, 1) \to (3, 2)$.
  • Jump down $a=2$ tiles: $(3, 2) \to (1, 2)$.
  • Jump right $b=1$ tile: $(1, 2) \to (1, 1)$.
  • $\cdots$
翻译中…

In this case, the tiles $\{(2, 1), (2, 2), (4, 1), (4, 2)\}$ remain unvisited forever. Since not all tiles can be covered, the answer is "NO". It can be shown that even if Prakul started by moving right in the first move, the answer would not have changed.

翻译中…

样例

Input #1
10
1 1 1 1
2 2 1 1
4 2 2 1
6 9 6 7
67 42 42 67
3411 4134 32 23
90234 143124 232 323
69387963 98793214 9791 4324786
985865 578977 899368 447605
1000000000 1000000000 1000000000 1000000000
Output #1
YES
YES
NO
NO
YES
NO
NO
NO
YES
NO
InputCopy OutputCopy
题目信息
题号
2217C
时间限制
2 s
内存限制
256 MB
标签
中国剩余定理
数学
数论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。