链接点这儿

题目:

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. JDK源码分析系列02---ArrayList和LinkList

    ArrayList和LinkList的源码分析 概要 ArrayList和LinkList是常用的存储结构,不看源码先分析字面意思,Array意思是数组,可知其底层是用数组实现的,Link意思是链接, ...

  2. 『开发技巧』Keras自定义对象(层、评价函数与损失)

    1.自定义层 对于简单.无状态的自定义操作,你也许可以通过 layers.core.Lambda 层来实现.但是对于那些包含了可训练权重的自定义层,你应该自己实现这种层. 这是一个 Keras2.0  ...

  3. GitHub代码阅读神器,你值有拥有!

    (题图:from  github) Github作为全球最大的程序员聚集地,已经成为学习开发技能的绝佳伴侣(如果你是程序员,但你还没有账户的话,这里建议你去signup,毕竟能增加成长的机会,不能错过 ...

  4. 手动启动log4j|nginx实现http https共存

    手动加载log4j.xml文件 DOMConfigurator.configure("src/main/resources/log4j.xml"); log4j.propertie ...

  5. web文件下载(附方案及源码配置)

    1. 场景描述 因项目需查询数据量比较大(需要查询Hbase等nosql数据库),采用用户点击查询后,后台查询并生成查询文件:然后消息通知用户后,用户点击下载的方式来满足用户需求. 2. 解决方案 W ...

  6. CMinpack使用介绍

    github: https://github.com/devernay/cminpack 主页: http://devernay.github.io/cminpack/ 使用手册: http://de ...

  7. 第三章.定制专属的kali

    1.更新升级 • apt-get update • apt-get upgrade • apt-get dis-upgrade   2.根据个人喜好需求安装软件包 • 库 • Apt-get命令 • ...

  8. Spring Boot微服务电商项目开发实战 --- 基础配置及搭建

    根据SpringBoot实现分布式微服务项目近两年的开发经验,今天决定开始做SpringBoot实现分布式微服务项目的系列文章,帮助其他正在使用或计划使用SringBoot开发的小伙伴们.本次系列文章 ...

  9. MyBatis 一对多映射

    From<MyBatis从入门到精通> <!-- 6.1.2.1 collection集合的嵌套结果映射 和association类似,集合的嵌套结果映射就是指通过一次SQL查询将所 ...

  10. 【EdgeBoard体验】开箱与上手

    简介 市面上基于嵌入式平台的神经网络加速平台有很多,今天给大家带来是百度大脑出品的EdgeBoard.按照官网文档的介绍,EdgeBoard是基于Xilinx Zynq Ultrascale+ MPS ...