#P11846. [USACO25FEB] Transforming Pairs P

    ID: 11488 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数学USACO2025最大公约数 gcd分类讨论

[USACO25FEB] Transforming Pairs P

Description

Answer QQ (1Q1051\le Q\le 10^5) independent queries each of the following form:

You are given four integers a,b,c,da,b,c,d (1018a,b,c,d1018-10^{18}\le a,b,c,d\le 10^{18}). In one operation you can either do a+=ba\mathrel{+}=b, or b+=ab\mathrel{+}=a. Determine the minimum number of operations to transform (a,b)(a,b) into (c,d)(c,d), or if it is impossible to do so, output 1-1.

Input Format

The first line contains QQ.

The next QQ lines each contain four integers a,b,c,da,b,c,d.

Output Format

The answer for each query on a separate line.

4
5 -3 -1 -3
5 3 5 2
5 3 8 19
5 3 5 3
2
-1
3
0

Hint

First query: (5,3)(2,3)(1,3)(5,-3)\to (2,-3)\to (-1,-3)

Second query: Impossible.

Third query: (5,3)(8,3)(8,11)(8,19)(5,3) \to (8, 3) \to (8, 11) \to (8, 19)

Fourth query: No operations necessary.

SCORING:

  • Input 2: a,b,c,d10|a|, |b|, |c|,|d|\le 10
  • Input 3: a,b0a,b\ge 0
  • Input 4: a0ba \geq 0 \geq b
  • Input 5: a0ba \leq 0 \leq b
  • Input 6: a,b0a,b\le 0
  • Input 7: c,d0c,d\ge 0
  • Input 8: c0dc \geq 0 \geq d
  • Input 9: c0dc \leq 0 \leq d
  • Input 10: c,d0c,d\le 0
  • Inputs 11-14: Q103Q \leq 10^3
  • Inputs 15-19: No additional constraints.