题目链接:https://codeforces.com/contest/1152/problem/C

题解:不妨设a<ba < b 那么$$lcm(a + k, b + k) = \frac {(a + k)\times (b + k)} {gcd(a + k, b + k)} $$
可推

lcm(a+k,b+k)=(a+k)×(b+k)gcd(a+k,ba)lcm(a + k, b + k)= \frac {(a+k) \times (b + k)} {gcd(a + k, b - a)}

不妨令
$$ t = a + k$$
$$ d = b - a$$

带入可$$lcm = \frac{t^2 + dt} {gcd(t, d)}$$

可以发现 分式的分子在0\geq 0时单调递增
考虑$ t \geq d 时 可能的情况,要么是td的倍数,要么td互质。 那么只需要考虑t$最小的两种情况即可。

考虑t<dt < d时, 只需要枚举dd的因子数,然后统计答案即可。

注意INF要设的够大

#include <bits/stdc++.h>
#define pa pair<ll, ll>
#define mp make_pair
#define lowbit(x) ((x)&(-x))
#define mem(i, a) memset(i, a, sizeof(i))
#define sqr(x) ((x)*(x))
#define ls (k << 1)
#define rs (k << 1 | 1)
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T &X) {
    X = 0; char ch = 0; T op = 1;
    for(; ch > '9' || ch < '0'; ch = getchar())
        if(ch == '-') op = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        X = (X << 3) + (X << 1) + ch - 48;
    X *= op;
}

const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll N = 1e6 + 5;
ll a,b,d,ans = INF,k;
ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}
ll cal(ll t) {
    return (sqr(t) + d * t) / gcd(t, d);
}
ll jud(ll now) {
    if(cal(now) <= ans) {
        if(cal(now) == ans) k = now - a;
        else
            ans = cal(now), k = now - a;
    }
}
int main() {
#ifdef INCTRY
    freopen("input.txt", "rt", stdin);
#endif
    cin >> a >> b;
    if(a > b) swap(a, b);
    d = b - a;
    if(d == 0) {
        cout << 0; return 0;
    }
    ll now = (a + d - 1) / d * d;
    jud(now);
    if(now == a) now++;
    else now--;
    jud(now);
    if(2 * a - b < 0) {
        for(ll i = 1; i <= sqrt(d); i++) {
            if(d % i == 0) {
                ll t1 = i;
                ll t2 = d / i;
                if(t1 >= a) jud(t1);
                if(t2 >= a) jud(t2);
            } else {
                jud(i);
            }
        }
    }
    cout << k;
#ifdef INCTRY
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}