uva—gcd lcm
来源:互联网 发布:lua编辑器 for mac 编辑:程序博客网 时间:2024/06/07 12:59
IIU C ONLINE C ON TEST 2 008
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 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 integersa and b, a ≤ b, which has a GCD ofG and LCM of L. In case there is more than one pair satisfying the condition, output the pair for whicha 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
题意:给出了gcd(最大公约数),lcm(最小公倍数),求满足gcd,lcm的两个数n,m,输出最小的一组解析:设n=a*b*c*d,m=a*b*c*e,那么易知gcd=a*b*c,lcm=a*b*c*d*e;即lcm%gcd==0.
再逆向思考给出gcd=a*b*c,lcm=a*b*c*d*e.那么有以下几种情况:
1.n=a*b*c,m=a*b*c*d*e
2.n=a*b*c*d,m=a*b*c*e
3.n=a*b*c*e,m=a*b*c*d
4.n=a*b*c*d*e,m=a*b*c
即d和e是可以自由排列的,且明显第一组是最小的。
if(l%g==0) cout<<g<<" "<<l<<endl; else cout<<"-1"<<endl;代码:
#include <iostream>using namespace std;int main(){ int t; cin>>t; while(t--) { int g,l; cin>>g>>l; if(l%g==0) cout<<g<<" "<<l<<endl; else cout<<"-1"<<endl; }}
- uva—gcd lcm
- UVA 11388 GCD LCM——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
- UVA-11388 GCD LCM
- UVA 11388 GCD LCM (数论)
- 线段树求矩形面积并 方法详解 (扫描线)HDU 1542 & HDU 3265 & POJ 1151
- 头部显示提示,如tableview滑动提示滑回顶部
- Gabor filter
- java web 之 jdk+mysql+tomcat+myeclipse配置部署项目注意事项
- MFC控件 listcontrol 控件使用总结
- uva—gcd lcm
- JS中的UniCode转码问题
- 考试系统之“实战判分”
- 互联网产品之美
- 层次遍历二叉树-三种不同的方法
- Sicily 1395. Rounders
- android设置edittext光标的颜色
- 20150126Review
- 初次见面 你好EF