• [ 东莞市选 2008 ] GCD&LCM


    (\)

    (Description)


    给出两数的(GCD)(LCM),求合法的两数之差的绝对值最小是多少。

    • (GCD imes LCMle10^{18})

    (\)

    (Solution)


    多解的有趣小水题。

    (\)

    解法一:求出(GCD imes LCM),我们知道这个就等于两数之积,考虑枚举其中的一个数。

    考虑枚举的数一定是(GCD)的倍数,所以直接枚举就好,我们只需要处理枚举的数小于另一个数的情况,最后将所有算出来的答案取(min) 即可,复杂度 ( ext O(sqrt{LCM}))

    (\)

    解法二:(frac{LCM}{GCD}=frac A{GCD} imes frac B{GCD})枚举第二个式子左半部分,乘上更新答案。复杂度( ext O(sqrt{frac{LCM}{GCD}}))

    (\)

    解法三:还是上面的式子。考虑当(frac A{GCD})(frac B{GCD})最接近的时候产生的差值最小所以直接从(sqrt{frac{LCM}{GCD}})处开始枚举第一个遇见的答案一定是最优秀的。

    (\)

    (Code)


    #include<cmath>
    #include<cstdio>
    #include<cctype>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define R register
    using namespace std;
    typedef long long ll;
    
    ll a,b,ans=900000000000000ll;
    
    inline ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
    
    int main(){
      scanf("%lld%lld",&a,&b);
      b*=a;
      for(R ll i=a,j;i<=b;i+=a){
        if(b%i!=0) continue;
        j=b/i; if(i>j) break;
        if(gcd(i,j)==a) ans=min(ans,j-i);
      }
      printf("%lld
    ",ans);
      return 0;
    }
    
  • 相关阅读:
    elk6.3 centos集群搭建 head插件安装
    10.2半群,同余关系,半群直积,商半群
    10.1代数结构
    9.4 关系的闭包
    9.5 等价关系
    9.6偏序关系
    9.3 关系的表示
    9.1 关系及关系性质
    差分数组
    拓扑排序
  • 原文地址:https://www.cnblogs.com/SGCollin/p/9758244.html
Copyright © 2020-2023  润新知