P8255
Analysis
首先显然需要把
设
于是对于任意的
- 若
\min(d_i, e_i) = e_i ,则e_i + \min(d_i, e_i) = 2e_i = c_i \Rightarrow e_i = \frac{c_i}{2} 。因为e_i \leq d_i ,所以c_i \leq 2d_i - 若
\min(d_i, e_i) = d_i ,则e_i + \min(d_i, e_i) = e_i + d_i = c_i \Rightarrow e_i = c_i - d_i 。因为e_i \gt d_i ,所以c_i \gt 2d_i 。
同时可以发现,
考虑把分母和减号消掉,于是
注意到两数的 lcm 就是他们对应指数的最大值,
于是
Code
实现上,注意到
#include <cmath>
#include <numeric>
#include <iostream>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int T;
for (std::cin >> T; T; --T) {
long long x, t;
std::cin >> x >> t;
if (t % x != 0) {
std::cout << "-1\n"; continue;
}
t /= x;
long long g = std::gcd(x * x, t), s = sqrt(g) + 0.5;
if (s * s != g) {
std::cout << "-1\n"; continue;
}
std::cout << t / s << '\n';
}
}