/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 625 点
問題文
整数 A,B,X,Y が与えられます。
二次元平面上にコマが置かれています。はじめコマは座標 (0,0) にあります。
あなたは以下の操作を 0 回以上何回でも行うことができます:
- 今コマがある座標を (x,y) として、以下の条件のいずれかを満たす座標 (x',y') にコマを移動させる。
- |x-x'|=A かつ |y-y'|=B
- |x-x'|=B かつ |y-y'|=A
コマを座標 (X,Y) に移動させることが可能か判定し、可能であれば必要な操作回数の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 2\times 10^5
- 1\le A < B \le 10^6
- 0\le X,Y\le 10^6
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
i 番目 (1\le i\le T) のテストケース \text{case}_i は以下の形式で与えられる。
A B X Y
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、コマを座標 (X,Y) に移動させることが可能な場合は操作回数の最小値を、不可能な場合は -1 を出力せよ。
入力例 1
4 1 2 1 0 4 6 3 5 459 2026 0 0 19 2026 523 459459
出力例 1
3 -1 0 2670
1 番目のテストケースについて考えます。
コマを座標 (0,0) から順に (1,2),(-1,1),(1,0) へと動かすことで 3 回の操作でコマを座標 (1,0) に移動させることができます。
3 回未満の操作でコマを座標 (1,0) に移動させることはできないので、1 行目には 3 を出力してください。
Score : 625 points
Problem Statement
You are given integers A, B, X, Y.
A piece is placed on a two-dimensional plane. Initially, the piece is at coordinates (0, 0).
You can perform the following operation zero or more times:
- Let (x, y) be the current coordinates of the piece. Move the piece to coordinates (x', y') satisfying one of the following conditions:
- |x - x'| = A and |y - y'| = B
- |x - x'| = B and |y - y'| = A
Determine whether it is possible to move the piece to coordinates (X, Y), and if so, find the minimum number of operations required.
You are given T test cases; solve each.
Constraints
- 1 \le T \le 2 \times 10^5
- 1 \le A < B \le 10^6
- 0 \le X, Y \le 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
The i-th (1 \le i \le T) test case \text{case}_i is given in the following format:
A B X Y
Output
Output the answers for the test cases in order, separated by newlines.
For each test case, if it is possible to move the piece to coordinates (X, Y), output the minimum number of operations required; otherwise, output -1.
Sample Input 1
4 1 2 1 0 4 6 3 5 459 2026 0 0 19 2026 523 459459
Sample Output 1
3 -1 0 2670
Consider the first test case.
By moving the piece from (0, 0) to (1, 2), then to (-1, 1), then to (1, 0) in order, the piece can be moved to coordinates (1, 0) in three operations.
It is impossible to move the piece to coordinates (1, 0) in fewer than three operations, so output 3 on the first line.