II
U 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 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

题意: 给你两个数的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,由于d和e的位置能够排列。要唯一的话,一定是lcm是gcd的倍数,且这对就是最小的

#include <iostream>
#include <cstdio>
#include <cstdio>
#include <cstring>
typedef long long ll;
using namespace std; int main() {
int t, n, m;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
if (m % n == 0)
printf("%d %d\n", n, m);
else printf("-1\n");
}
return 0;
}

UVA - 11388 GCD LCM的更多相关文章

  1. UVA 11388 - GCD LCM 水~

    看题传送门 题目大意: 输入两个数G,L找出两个正整数a 和b,使得二者的最大公约数为G,最小公倍数为L,如果有多解,输出a<=b且a最小的解,无解则输出-1 思路: 方法一: 显然有G< ...

  2. UVa 10892 (GCD) LCM Cardinality

    我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会. 最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数 #include <cstdio> ...

  3. Mathematics:GCD & LCM Inverse(POJ 2429)

    根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...

  4. 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 ...

  5. [POJ 2429] GCD & LCM Inverse

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10621   Accepted: ...

  6. POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)

    [题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...

  7. UVA 11426 - GCD - Extreme (II) (数论)

    UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...

  8. hdu-3071 Gcd & Lcm game---质因数分解+状态压缩+线段树

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3071 题目大意: 给定一个长度为n的序列m次操作,操作的种类一共有三种 查询 L :查询一个区间的所 ...

  9. [ 9.13 ]CF每日一题系列—— 340A GCD & LCM

    Description: [ 着实比较羞愧,都想着去暴力,把算法(方法)也忘了] A只涂x,2x,3x……,B只涂y,2y,3y……问你A和B共同涂的墙的个数 Solution: 就是求x和y的lcm ...

随机推荐

  1. PHP - 判断php是否对表单数据内的特殊字符自动转义

    get_magic_quotes_gpc 有两个返回值: 0:在php.ini文件中已经关闭自动转移. 1:在php.ini文件中已经开启自动转移. 由此函数进行判断表单是否转移: /** * * m ...

  2. Delphi的RTTI还分为对类和对象的判断,以及对普通属性的判断——相比之下,C++的RTTI实在太弱!

    堂堂C++沦落到这个地步,也实在是够可怜的.

  3. Hashtable的使用

    Hashtable mylist = new Hashtable();             mylist.Add("1", "100");          ...

  4. France '98

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30506#problem/H #include<map> #include&l ...

  5. 细节!重点!易错点!--面试java基础篇(二)

    今天来给大家分享一下java的重点易错点第二部分,也是各位同学面试需要准备的,欢迎大家交流指正. 1.字符串创建与存储机制:当创建一个字符串时,首先会在常量池中查找是否已经有相同的字符串被定义,其判断 ...

  6. unix domain IPC 进程间通信简析

    Linux系统有多种进程间通信方式,如信号.消息队列.管道等,socket是其中一种,socket使用unix domain 模式进行进程间通信 //服务端代码 #include <stdio. ...

  7. 基于visual Studio2013解决面试题之0308Fibonacci数列

     题目

  8. OpenVPN多处理之-netns容器与iptables CLUSTER

    假设还是沉湎于之前的战果以及强加的感叹,不要冥想,将其升华. 1.C还是脚本 以前,我用bash组织了复杂的iptables,ip rule等逻辑来配合OpenVPN,将其应用于差点儿全部能够想象得到 ...

  9. pig对null的处理(实际,对空文本处理为两种取值null或‘’)

    pig对文本null的处理非常特殊.会处理成两种null,还会处理成''这样的空值. 比方,读name,age,sex日志信息.name取值处理,假设记录为".,,"这样,会将na ...

  10. 插件 - 提示窗体(ArtDialog)

    效果: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...