链接点这儿

题目:

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 2 1 2 3 4

Sample Output 1 2 -

题目大意:

其实就是说,给你两个数的GCD和LCM,让你求一种小的那个数尽可能的小,并大的在这个基础上也尽可能的小的那两个数。

先上代码!

 #include<bits/stdc++.h>
using namespace std;
int t;
long long a,b,c,d,ans[][];
int main()
{
cin>>t;
for(int i=;i<=t;i++)
{
cin>>a>>b;
if(b%a==)//如果GCD可被LCM整除
{
ans[i][]=a;
ans[i][]=b;
continue;
}
else ans[i][]=-;
}
for(int i=;i<=t;i++)
if(ans[i][]==-)
printf("-1\n");
else
printf("%lld %lld\n",ans[i][],ans[i][]);
return ;
}

为什么可以这么写呢?下面我详细讲一讲:

证明:

我们先看到第11行的if语句:当GCD可被LCM整除时,两个数为GCD和LCM。

我们设两个数分别为a,b。(a<=b)

首先我们知道,GCD一定可以被a和b整除,而a和b又可以被LCM整除,所以GCD一定可被LCM整除。

而我们又知道,当一个数x可被y整除时,他们的GCD为x,LCM为y

又因为GCD*k=a(k>=1&&k==int(k)),所以a>=GCD,最小值为GCD。

所以我们这里就设a为最小值。(也就是a=GCD)

我们确定了a以后,又根据公式:GCD*LCM=a*b,其中GCD,a,LCM已知,所以b的值一定是固定的,而且就等于LCM。

所以当a取最小值(a=GCD(a,b),b=LCM(a,b))时,(a,b)为符合要求的最优解。

证毕。

第11行证完了之后,我们再来证第17行的else语句。

其实还是利用GCD一定可以被a和b整除,而a和b又可以被LCM整除,所以GCD一定可被LCM整除这个原理。

所以一定无解。

证毕。

这个时候我们就证完了。O(t)算法(其实可以算是O(1))。

如果你觉得你有更巧妙的方法,欢迎在下方留言。

UVA11388 GCD LCM的更多相关文章

  1. 洛谷 UVA11388 GCD LCM

    UVA11388 GCD LCM Description of the title PDF The GCD of two positive integers is the largest intege ...

  2. UVA11388 GCD LCM(数论)

    题目链接. 题意: 给定两个数,一个G,一个L,找出两个数a,b(a<=b),使得这两个数的最大公约数为G,最小公倍数为L,且(a最小). 分析: 当a,b存在时,a一定为G. 自己证了一下,数 ...

  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 - 11388 GCD LCM

    II U C   ONLINE   C ON TEST  Problem D: GCD LCM Input: standard input Output: standard output The 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. C++ 洛谷 P2704 [NOI2001]炮兵阵地

    P2704 [NOI2001]炮兵阵地 没学状压DP的看一下 此题意思很简单,如下图,就是十字架上的不能有两个点放炮兵. 在做此题前,先做一下玉米田 玉米田题解 分析: 而m即一行的个数小于等于10, ...

  2. HDU 4462:Scaring the Birds(暴力枚举+状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=4462 题意:有一个n*n的地图,有k个空地可以放稻草人,给出每个空地可以放的稻草人属性,属性中有个R代表这个位置 ...

  3. 自动化冒烟测试 Unittest , Pytest 哪家强?

    前言:之前有一段时间一直用 Python Uittest做自动化测试,觉得Uittest组织冒烟用例比较繁琐,后来康哥提示我使用pytest.mark来组织冒烟用例 本文讲述以下几个内容: 1.Uni ...

  4. Elasticsearch(一)开启外网访问

    1. 设置Elasticsearch对外访问的Host 修改Elasticsearch配置文件 elasticsearch.yml : network.host: 128.24.108.84  //在 ...

  5. windows切换mac遇到的问题

    1. 前端代码需要安装npm包 所以需要对整个文件夹都赋予管理员权限 2. 在npm i的时候如果权限不足 查看是哪一行调用了哪个文件夹,赋予权限 3. Dsp-fe 本地环境 除了需要配置host  ...

  6. 0x31 prime distance(质数)

    题目描述: 给定两个整数L和U,你需要在闭区间[L,U]内找到距离最接近的两个相邻质数C1和C2(即C2-C1是最小的),如果存在相同距离的其他相邻质数对,则输出第一对. 同时,你还需要找到距离最远的 ...

  7. 5.Ray-Handler之ToReadHandler编写

    如图右上角所示,Ray中有两类Handler(SubHandler和PartSubHandler),在使用中,SubHandler派生Actor的CoreHandler,PartSubHandler派 ...

  8. SpringBoot快速入门01--环境搭建

    SpringBoot快速入门--环境搭建 1.创建web工程 1.1 创建新的工程. 1.2  选择maven工程,点击下一步. 1.3 填写groupid(maven的项目名称)和artifacti ...

  9. JAVA获取公网ip

    在ipv4地址稀缺的今天,分配到公网ip几乎是不可能的,但是我拨号之后的ip竟然是公网IP. 将自己的电脑作为服务器·,做点好玩的程序,就成为了可能. 由于运营商的ip是动态分配的公网ip的所以就需要 ...

  10. o2优化(手动)

    #pragma GCC optimize(2) 将这句话放到程序开头即可