题解:P18115 [NOIP2026] 和弦 / chord
由于我们更熟悉
枚举
所以先对
特判
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
typedef long long ll;
typedef unsigned long long ull;
int c, t;
ll a, b;
inline ll calc(ll d, ll B, ll D) {
ll k = (B + d - 1) / d;
return k * (D + k * d);
}
inline void solve() {
std::cin >> a >> b;
if(a == b) { std::cout << a << '\n'; return; }
ll A = std::max(a, b), B = std::min(a, b), D = A - B;
ll ans = A * B;
for(ll d = 1; d * d <= D; ++d) {
if(D % d) continue;
ans = std::min(ans, calc(d, B, D));
if(d * d != D) ans = std::min(ans, calc(D / d, B, D));
}
std::cout << ans << '\n';
}
int main() {
// std::freopen("musicbox.in", "r", stdin);
// std::freopen("musicbox.out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> c >> t;
while(t--) solve();
return 0;
}