C - GCD LCM
Description
The GCD of two positive integers is the largest integer that divides both the integers without any remainder. The LCM of two positive integers is the smallest positive integer that is divisible by both the integers. A positive integer can be the GCD of many pairs of numbers. Similarly, it can be the LCM of many pairs of numbers. In this problem, you will be given two positive integers. You have to output a pair of numbers whose GCD is the first number and LCM is the second number.
Input
The first line of input will consist of a positive integer T. T denotes the number of cases. Each of the next T lines will contain two positive integer, G and L.
Output
For each case of input, there will be one line of output. It will contain two positive integers a and b, a ≤ b, which has a GCD of G and LCM of L. In case there is more than one pair satisfying the condition, output the pair for which a is minimized. In case there is no such pair, output ‘-1’.
Constraints
• T ≤ 100
• Both G and L will be less than 2^31
Sample Input
2
1 2
3 4
Sample Output
1 2
-1
解题思路:题目的意思就是给出某两个数的最大公因数G和最小公倍数L,求出这两个数中一个最小和一个最大。推导一下可知:两个数中最小的那个数至少为这两个数的最大公约数,则最大的数为这两个数的最小公倍数。假设这两个数为a,b,则G*L=a*b,因为G最小,而a*b的值一定,即L最大,所以原来的两个数中最小的数为G,最大的数为L。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,g,l;
cin>>t;
while(t--){
cin>>g>>l;
if(l%g!=)cout<<-<<endl;
else cout<<g<<' '<<l<<endl;
}
return ;
}
C - GCD LCM的更多相关文章
- Mathematics:GCD & LCM Inverse(POJ 2429)
根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
- POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)
[题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...
- UVA - 11388 GCD LCM
II U C ONLINE C ON TEST Problem D: GCD LCM Input: standard input Output: standard output The GC ...
- hdu-3071 Gcd & Lcm game---质因数分解+状态压缩+线段树
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3071 题目大意: 给定一个长度为n的序列m次操作,操作的种类一共有三种 查询 L :查询一个区间的所 ...
- [ 9.13 ]CF每日一题系列—— 340A GCD & LCM
Description: [ 着实比较羞愧,都想着去暴力,把算法(方法)也忘了] A只涂x,2x,3x……,B只涂y,2y,3y……问你A和B共同涂的墙的个数 Solution: 就是求x和y的lcm ...
- 【HDU 5382】 GCD?LCM! (数论、积性函数)
GCD?LCM! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- 数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho
数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p ...
- 数论3——gcd&&lcm
gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm ( gcd就是gcd(a, b), ( •̀∀•́ ) ...
随机推荐
- 选择器的使用(target选择器)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- 打造Spring Cloud构建微服务架构的最全资料
访问: https://git.oschina.net/didispace/SpringCloud-Learning http://blog.didispace.com/categories/Spri ...
- psychology
壹.自身(荣.命) 一.职业分析 ㈠.分析性格→分析长处和短处→分析大家都有的长处→确定自己最终发展的专业. 1 .性格--宗正 耐压力特强,即使肩头责任重大,也能够处理得稳稳当当,是 ...
- apache ab 測试 apr_socket_connect(): 因为目标机器积极拒绝 无法连接
遇到这样的情况通常是你开的并行数量太多了... 比如:ab -c 1000 -n 10000 http://localhost/index.html 如此大的请求就会挂掉,只是还是有补救措施的,能够通 ...
- 斜率优化专题1——bzoj 1597 [Usaco2008 Mar] 土地购买 题解
转载请注明:http://blog.csdn.net/jiangshibiao/article/details/24387147 [原题] 1597: [Usaco2008 Mar]土地购买 Time ...
- WCF探索之旅(五)——WCF与WebService的异同
前几篇文章我们简单的介绍了WCF以及怎样使用它,今天我们来讨论一下WCF和WebService的异同. 相信大多数同学跟我一样,对于WebService有所了解.并且应该说你是先听说WebServic ...
- hdu 5256 序列变换
最长上升子序列 nlogn;也是从别人的博客学来的 #include<iostream> #include<algorithm> #define maxn 100000+5 u ...
- office文档、图片、音/视频格式转换工具
1.音频/视屏转换工具VLC https://wiki.videolan.org/Mp3/#Container_formats http://wenku.baidu.com/view/ba73ac5 ...
- ios30---pthread, NSThread, GCD, NSOperation
pthread(线程库,很早就有的技术,了解):一套通用的多线程API适用于Unix\Linux\Windows等系统(java开发也有pthread)跨平台\可移植使用难度大(全是C函数) C语言 ...
- java操作linux,调用shell命令
import org.junit.jupiter.api.Test; import java.io.BufferedReader; import java.io.IOException; import ...