Codeforces 1285C Fadi and LCM(GCD)

传送门

题意:

给一个数x,找到满足lcm(a,b)==x中,max(a,b)的值最小的那一对a,b

思路:

两个数尽可能接近且两个数互质,这样max(a,b)才会越小,那就从sqrt(x)开始枚举,找到符合的输出即可

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int MAXN=2e5+50;
const double pi=3.1415926536;
char ch[MAXN];
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
int main()
{
    //freopen("in.txt","r",stdin);
    ll x;
    scanf("%lld",&x);
    ll p=(long long)sqrt(x+0.5);
    ll k1,k2;
    for(ll i=p;i>=1;i--){
        if(x%i==0){
            k2=i;
            k1=x/k2;
            if(gcd(k1,k2)==1)
            break;
        }
    }
 
    if(k1>k2)swap(k1,k2);
    printf("%lld %lld\n",k1,k2);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zzl-dreamfly/p/12228822.html