题意
给连个数G,L,求出另外两个数a,b,使得gcd(a,b)=G,lcm(a,b)=L,如果有多种情况,输出a最小的情况
分析
简单分析一下可以得到gcd(G,L)=G,lcm(G,L)=L,那么会不会有其他的a比G更小呢,显然不会。因为G是a和其他数的最大公因数,只可能是G<=a.所以最后判断一下G能否整除L即可,不能则输出-1,否则输出G,L.
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
ios::sync_with_stdio(false);
int l, g, t;
cin >> t;
while (t--)
{
cin >> g >> l;
if (l%g == 0) cout << g << " " << l << endl;
else cout << -1 << endl;
}
return 0;
}
本文探讨了给定两个数G和L时,如何找出另外两个数a和b,使得a和b的最大公约数等于G,最小公倍数等于L。分析了G和L之间的关系,并给出了一种简便的算法实现,通过判断G是否能整除L来确定解的存在性。
2万+

被折叠的 条评论
为什么被折叠?



