码迷,mamicode.com
首页 > 其他好文 > 详细

temp

时间:2019-04-25 01:32:31      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:ret   取出   ace   long   txt   style   return   pre   color   

 

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

标签:ret   取出   ace   long   txt   style   return   pre   color   

原文地址:https://www.cnblogs.com/albert-biu/p/10765933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!