temp
Posted albert-biu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了temp相关的知识,希望对你有一定的参考价值。
g = gcd(a+k,b+k),设 a = g*x,b = g*y,则g(x-y) = a-b, 所以 g|(a-b),枚举a-b的所有约数,按照题意取出最小答案
#include "bits/stdc++.h" using namespace std; typedef long long ll; const int mod = 1e9 + 7; const int maxn = 1e5 + 100; ll a, b; vector<ll> yu; ll maxx = 0x3f3f3f3f3f3f3f3f; ll ans = 0x3f3f3f3f3f3f3f3f; int main() { //freopen("in.txt", "r", stdin); cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } if (__gcd(a, b) == a || __gcd(a, b) == b) { cout << 0 << endl; return 0; } if (a < b) swap(a, b); ll n = a - b; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { yu.push_back(i); if (i != n / i) yu.push_back(n / i); } } ll x, y, cha; for (auto g:yu) { cha = n / g; y = (b + g - 1) / g; if ((y + cha) * g >= a) { x = y + cha; if (g * x * y < maxx) { maxx = g * x * y; ans = g * x - a; } else if (g * x * y == maxx && g * x - a < ans) { ans = g * x - a; } } else { x = (a + g - 1) / g; y = x - cha; if (g * x * y < maxx) { maxx = g * x * y; ans = g * x - a; } else if (g * x * y == maxx && g * x - a < ans) { ans = g * x - a; } } } cout << ans << endl; return 0; }
以上是关于temp的主要内容,如果未能解决你的问题,请参考以下文章