题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Bessie the brilliant bovine has discovered a new fascination—mathematical magic! One day, while trotting through the fields of Farmer John’s ranch, she stumbles upon two enchanted piles of hay. The first pile contains $a$ bales, and the second pile contains $b$ bales ($1\le a,b\le 10^{18}$).
Next to the hay, half-buried in the dirt, she discovers an ancient scroll. As she unfurls it, glowing letters reveal a prophecy:
To fulfill the decree of the Great Grasslands, the chosen one must transform these two humble hay piles into exactly $c$ and $d$ bales—no more, no less.
Bessie realizes she can only perform the following two spells:
- She can magically conjure new bales to increase the first pile’s size by the amount currently in the second pile.
- She can magically conjure new bales to increase the second pile’s size by the amount currently in the first pile.
For each of $T$ ($1\le T\le 10^4$) independent test cases, output the minimum number of operations needed to fulfill the prophecy, or if it is impossible to do so, output -1.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $T$.The next $T$ lines each contain four integers $a,b,c,d$.
OUTPUT FORMAT (print output to the terminal / stdout):
Output $T$ lines, the answer to each test case.SAMPLE INPUT:
4 5 3 5 2 5 3 8 19 5 3 19 8 5 3 5 3
SAMPLE OUTPUT:
-1 3 -1 0
In the first test case, it is impossible since $b>d$ initially, but operations can only increase $b$.
In the second test case, initially the two piles have $(5, 3)$ bales. Bessie can first increase the first pile by the amount in the second pile, resulting in $(8, 3)$ bales. Bessie can then increase the second pile by the new amount in the first pile, and do this operation twice, resulting in $(8, 11)$ and finally $(8, 19)$ bales. This matches $c$ and $d$ and is the minimum number of operations to get there.
Note that the third test case has a different answer than the second because $c$ and $d$ are swapped (the order of the piles matters).
In the fourth test case, no operations are necessary.
SAMPLE INPUT:
1 1 1 1 1000000000000000000
SAMPLE OUTPUT:
999999999999999999
SCORING:
- Inputs 3-4: $\max(c, d) \le 20 \cdot\min(a, b)$
- Inputs 5-7: $T \le 10$ and $a,b,c,d\le 10^6$
- Inputs 8-12: No additional constraints
Problem credits: Benjamin Qi
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 8.0k平均输出 token(含思考) ↓
- 34.7输出速度 tokens/s ↑
- 233 秒延迟 ↓
- 0.74×CPU 时间 / 官方解 ↓
- 不可测时间复杂度(相对官方解)
- 4Pylint 问题 ↓
- 9.02Pylint 得分 ↑
- 15圈复杂度 ↓
- 55.8可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 9.6k平均输出 token(含思考) ↓
- 204.3输出速度 tokens/s ↑
- 48 秒延迟 ↓
- 0.68×CPU 时间 / 官方解 ↓
- 不可测时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.17Pylint 得分 ↑
- 13圈复杂度 ↓
- 64.0可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 16.0k平均输出 token(含思考) ↓
- 179.9输出速度 tokens/s ↑
- 89 秒延迟 ↓
- 0.69×CPU 时间 / 官方解 ↓
- 不确定时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.12Pylint 得分 ↑
- 17圈复杂度 ↓
- 65.9可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def min_operations(a, b, c, d): if c < a or d < b: return -1 x, y = c, d operations = 0 # Reverse the operations. Outside the initial state, the larger # coordinate must have been the one most recently increased. while x != a or y != b: if x < a or y < b or x == y: return -1 if x == a: difference = y - b if difference % a: return -1 return operations + difference // a if y == b: difference = x - a if difference % b: return -1 return operations + difference // b if x > y: count = (x - 1) // y x -= count * y else: count = (y - 1) // x y -= count * x operations += count return operations def main(): data = sys.stdin.buffer.read().split() t = int(data[0]) answers = [] index = 1 for _ in range(t): a = int(data[index]) b = int(data[index + 1]) c = int(data[index + 2]) d = int(data[index + 3]) index += 4 answers.append(str(min_operations(a, b, c, d))) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys def min_operations(a, b, c, d): if c < a or d < b: return -1 x, y = c, d operations = 0 while True: if x == a and y == b: return operations if x == y: return -1 if x > y: # Reverse: subtract y from x as many times as possible # without making x smaller than a. k = (x - a) // y if k == 0: return -1 x -= k * y operations += k else: # Reverse: subtract x from y as many times as possible # without making y smaller than b. k = (y - b) // x if k == 0: return -1 y -= k * x operations += k def main(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return t = data[0] idx = 1 answers = [] for _ in range(t): a, b, c, d = data[idx:idx + 4] idx += 4 answers.append(str(min_operations(a, b, c, d))) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys def minimum_operations(a: int, b: int, c: int, d: int) -> int: x, y = c, d operations = 0 while True: # We have reached the initial pair. if x == a and y == b: return operations # Reverse operations never increase a coordinate. # Equal piles have no valid predecessor. if x < a or y < b or x == y: return -1 if x > y: # The initial pair may occur before the end of this # batch of subtractions. if y == b and x >= a and (x - a) % y == 0: return operations + (x - a) // y count = (x - 1) // y x -= count * y operations += count else: # Symmetric case: repeatedly subtract x from y. if x == a and y >= b and (y - b) % x == 0: return operations + (y - b) // x count = (y - 1) // x y -= count * x operations += count def solve() -> None: data = list(map(int, sys.stdin.buffer.read().split())) if not data: return t = data[0] index = 1 answers = [] for _ in range(t): a, b, c, d = data[index:index + 4] index += 4 answers.append(str(minimum_operations(a, b, c, d))) sys.stdout.write("\n".join(answers) + "\n") if __name__ == "__main__": solve()