lcm与gcd的一些题

\(给出lcm(a,b),最小化max(a,b)\)

传送门

\(lcm(a,b) = a * b /gcd(a,b)\)

\(lcm(a,b) \% a == 0 \ \ \&\& \ \ lcm(a,b) \% b == 0\)

\(gcd(a,b) == 1\)时,有\(lcm(a,b) = a * b\)

给定\(lcm(a,b)\)最小化\(max(a,b)\)为什么要\(gcd(a,b) == 1时\)成立呢?

\(gcd(a,b) != 1\) \(a * b = lcm(a,b) * gcd(a,b)\)

\(a + b ≥ 2ab\) 最小化\(a*b\)即可,即最小化\(gcd(a,b)\)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 1e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll ans = inf;
ll gcd(ll a,ll b){
    return b == 0?a:gcd(b,a % b);
}
void solve(ll x,ll m){
    for(ll i = 1; i <= m; i++){
        if(x % i)continue;
        if(gcd(i,x / i) != 1)continue;
        if(max(i,x / i) < ans){
            ans = max(i,x/i);
        }
    }
    printf("%lld %lld\n",ans,x/ans);
}
int main(){
    scanf("%lld",&x);
    m = sqrt(x);
    solve(x,m);
    return 0;
}

\(给出lcm(a,b)和a,求最小b\)

猜你喜欢

转载自www.cnblogs.com/Emcikem/p/12179224.html
今日推荐