codeforces 613 div2 Fadi and LCM(LCM和GCD的关系 因子分解)

题目大意:

给出一个数X 找出数字A,B满足:

LCM(A,B) == X

max(A,B) 最小 

解题思路:首先 我们知道 A,B必须是X的因子。因为根据公倍数的概念,X=k1*A; X = k2 *B 。其中k1 k2是常数。那么我们就开始遍历X的因子咯 注意以前的因子分解都是从2到sqrt(x),现在因为需要max(A,B)最小,所以我们从sqrt(x)->2这里找。假如找到因子了就可以退出了,因为我们可以考虑最理想的因子肯定不是1和X这两个极端,max(A,B)==X 是最坏的结果,所以肯定是从sqrt(x)开始往后找才是最优的。

注意:这里我们必须理解一个概念,因子和质因子。因子和质因子分解的代码是不一样的!

#include <bits/stdc++.h>
#define int long long 
using namespace std;
int32_t main(){
	int x;cin>>x;
	for(int i=sqrt(x);i>=2;i--){
		if(x%i==0){
			int nx=x/i;
			if(__gcd(nx,i)==1){
				cout<<i<<" "<<nx<<endl;
				return 0;
			}
		}
	
	}
	cout<<1<<" "<<x<<endl;
	return 0;
}
发布了171 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/FrostMonarch/article/details/103934864