Codeforces Round #613 (Div. 2)——C. Fadi and LCM

题目大意:

题目传送门:https://codeforces.com/contest/1285/problem/C
给你一个数字n,这个数字是某两个数字(假设为a,b)的最小公倍数,求所有可能的max(a,b)的最小值。并输出此时的a,b
例如:1 6 和 2 3 的最小公倍数都是6,而max(1,6)=6>max(2,3)=3
所以我们要输出2 3.

题目分析:

最小公倍数满足以下性质:
a * b = gcd(a,b)* lcm(a,b)
现在lcm(a,b)已知,如果要令max(a,b)最小(a和b的差最小),也就是令a*b最大。换句话说,当gcd(a,b)=1也就是a,b互质的时候满足题意。所以我们只需要设置一个变量为sqrt(n),然后暴力寻找gcd(a,b)=1且lcm(a,b)=n即可。

AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#include<string> 
#include<vector>
#include<stack>
#include<queue> 
#define PI 3.1415926
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b) 
{
    if (b == 0) return a;
    else gcd(b, a % b);
}
int main() {
    ll n;
    cin>>n;
    ll x = sqrt(n);
    if (x * x == n) 
	x--;
    for (ll i = x; i >= 1; i--) {
        if (n % i == 0) {
            if (gcd(n / i, i) == 1) {
                printf("%lld %lld", i, n / i);
                return 0;
            }
        }
    }
    printf("1 %lld", n);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值