uva 11388 GCD LCM(简单数学)
来源:互联网 发布:php推送消息到app 编辑:程序博客网 时间:2024/05/22 17:23
Problem D: GCD LCM
Input: standard input
Output: standard output
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 Tlines 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 231.
Sample Input
Output for Sample Input
2
1 2
3 4
1 2
-1
Problem setter: Shamim Hafiz
题意:给你两个数G,L,让你求两个数a,b,使得gcd(a,b)=G,Lcm(a,b)=L;如果有多个答案输出a最小的,没有则输出-1;L=a*b/G=G*a1*b1;其中gcd(a1,b1)=1;令a1b1=L/G;然后枚举a1b1的约数,符合条件的即为答案;
代码:
#include <iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<vector>#include<stdlib.h>#include<string>#include<map>#include<math.h>#include<queue>#include<stack>#include<set>#define inf 0x3f3f3f3f#define eps 1e-5#define max(a,b) (a)>(b)?(a):(b)#define min(a,b) (a)<(b)?(a):(b)using namespace std;int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b);}int main(){ int t,g,i,l; while(~scanf("%d",&t)) { while(t--) { scanf("%d%d",&g,&l); if(l%g!=0) { printf("-1\n"); continue; } else { int ab=l/g; int k=sqrt(ab+0.0); bool flag=false; for(i=1;i<=k;i++) { if(ab%i==0) { if(i<=ab/i) { if(gcd(ab/i,i)==1) { flag=true; printf("%d %d\n",i*g,ab/i*g); break; } } } } if(!flag) printf("-1\n"); } } } return 0;}
- uva 11388 GCD LCM(简单数学)
- UVA 11388-GCD LCM(数学)
- uva 11388 GCD LCM(数学:水题)
- UVAOJ 11388 GCD LCM (简单数学)
- UVa 11388 - GCD LCM (简单数论)
- UVa 11388 - GCD LCM
- UVa 11388 - GCD LCM
- uva 11388 - GCD LCM
- UVA 11388 - GCD LCM
- UVa:11388 GCD LCM
- UVA - 11388 GCD LCM
- UVa 11388 - GCD LCM
- UVA 11388 GCD LCM
- UVa 11388----GCD LCM
- UVA 11388 GCD LCM
- uva 11388 GCD LCM
- UVA 11388 GCD LCM
- UVA - 11388 GCD LCM
- 常用Linux命令合集
- MFC单文档框架编程(二): SDI框架下的消息处理
- MFC单文档框架编程(三): CTabView的使用
- Struts 1 之<bean>标签库
- delphi 中的函数指针 回调函数
- uva 11388 GCD LCM(简单数学)
- Redis命令总结
- Eclipse 常用快捷键整理
- Linux环境下svn常用调试命令
- linux系统的sudo apt-get命令大全
- 接 口
- 当把目标定为买一栋楼时,买一套房就变得轻而易举,有朋友时,一起疯一起乐一起干活;一个人时,刻苦学习,攻读我的圣经。 不管你身处何方,小黄人软件都是你强大的后盾。
- 多线程并发常用类:condition,semaphore,CyclicBarrier,countdownlatch,exchanger使用整理
- 给大公司离职创业者的建议:转换心态和工作方法