#P10217. [省选联考 2024] 季风
[省选联考 2024] 季风
Background
Little X, who lives on a 2D plane, is going to visit Little Y. However, due to climate changes, a monsoon is blowing on the plane. Little X wants to know, under the influence of the monsoon, at least how many days it will take to reach Little Y’s home. Since Little X is also seeing this strange situation for the first time, please ask you, who are skilled in algorithms, to help.
Problem Description
Given and integers .
Find the minimum non-negative integer such that there exist real numbers satisfying the following conditions, or report that no such exists:
- $\sum \limits_{i=0}^{m-1} (x_i' + x_{i \bmod n}) = x$;
- $\sum \limits_{i=0}^{m-1} (y_i' + y_{i \bmod n}) = y$;
- .
In particular, when , both and are considered to be .
Input Format
This problem has multiple test cases. The first line contains an integer denoting the number of test cases.
For each test case,
- The first line contains four integers ;
- The next lines contain two integers each; the -th line contains .
Output Format
For each test case, output one integer per line. If there exists an satisfying the statement, output the minimum possible value; otherwise output .
4
1 2 2 2
1 1
1 2 -2 -2
1 1
1 2 0 0
1 1
2 100000000 100000000 100000000
-99999999 0
-100000000 0
1
-1
0
399999999
Hint
[Sample 1 Explanation]
This sample contains four test cases.
- For the first test case, take , , which satisfies the conditions. It can be proven that no smaller can satisfy the conditions;
- For the second test case, it can be proven that there is no non-negative integer that satisfies the conditions;
- For the third test case, take , which satisfies the conditions. It can be proven that no smaller can satisfy the conditions.
[Sample 2]
See the attached wind2.in/ans.
This sample contains eighty test cases. All testdata satisfy , where testdata satisfy Special Property A, satisfy Special Property B, and satisfy Special Property C.
[Sample 3]
See the attached wind3.in/ans.
This sample contains sixty test cases. All testdata satisfy , where testdata satisfy Special Property A, and satisfy Special Property B.
[Subtasks]
Let be the sum of over all test cases within a single test point. For all testdata:
- ;
- , ;
- .
| Test Point ID | Special Property | ||
|---|---|---|---|
| A | |||
| B | |||
| C | |||
| None | |||
| A | |||
| B | |||
| None | |||
| A | |||
| B | |||
| None |
- Special Property A: , ;
- Special Property B: ;
- Special Property C: .
[Hint]
The input file for this problem is large. Please use a faster input method.
Translated by ChatGPT 5