以下是使用 Python 解决此问题的代码: ```python import math def gcd(a, b): while b!= 0: a, b = b, a % b return a def solve(n, m): if m % n!= 0: return -1 count = 0 while n < m: for y in range(1, m + 1): new_n = n * gcd(n, y) if new_n > n and new_n <= m: n = new_n count += 1 break else: return -1 return count t = int(input()) for _ in range(t): n, m = map(int, input().split()) print(solve(n, m)) ``` 这段代码首先定义了一个求最大公因数的函数`gcd`,然后定义了`solve`函数来计算将`n`通过操作变为`m`所需的最少操作次数。在主程序中,读取测试数据的组数`t`,然后对每组数据调用`solve`函数并输出结果。 [2024-09-28 15:36:37 | AI写代码神器 | 256点数解答]