HDU - 1019-Least Common Multiple(求最小公倍数(gcd))
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.
Input
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.
Output
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
Sample Input
2
3 5 7 15
6 4 10296 936 1287 792 1
Sample Output
105
10296
题解:
利用一个数与另一个数的乘积等于这两个数的最小公倍数×最大公约数
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n;
long long int k;
for(int t=0;t<n;t++)
{
scanf("%d",&m);
long long int cnt=1;
for(int j=0;j<m;j++)
{
scanf("%d",&k);
cnt=cnt*k/(__gcd(cnt,k));
}
printf("%lld\n",cnt);
}
return 0;
}
HDU - 1019-Least Common Multiple(求最小公倍数(gcd))的更多相关文章
- HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】
Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU 1019 Least Common Multiple GCD
解题报告:求多个数的最小公倍数,其实还是一样,只需要一个一个求就行了,先将答案初始化为1,然后让这个数依次跟其他的每个数进行求最小公倍数,最后求出来的就是所有的数的最小公倍数.也就是多次GCD. #i ...
- ACM hdu 1019 Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- HDU 1019 Least Common Multiple 数学题解
求一组数据的最小公倍数. 先求公约数在求公倍数.利用公倍数,连续求全部数的公倍数就能够了. #include <stdio.h> int GCD(int a, int b) { retur ...
- HDU - 1019 - Least Common Multiple - 质因数分解
http://acm.hdu.edu.cn/showproblem.php?pid=1019 LCM即各数各质因数的最大值,搞个map乱弄一下就可以了. #include<bits/stdc++ ...
- HDU 1019 (多个数的最小公倍数)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (J ...
- 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)
作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...
- hdu 2028 Lowest Common Multiple Plus(最小公倍数)
Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDOJ 1019 Least Common Multiple(最小公倍数问题)
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
随机推荐
- 使用MPI进行分布式内存编程(2)
MPI的英文全称为message passing interface,中文名为消息传递接口,他不是一种新的语言,而是一个可以被C,C++,Fortran程序调用的库. 预备知识 1.编译与执行 使用类 ...
- Linux本地套接字(Unix域套接字)----SOCK_DGRAM方式
目录 简述 创建服务端代码: 创建客户端代码 接收函数封装 发送封装 服务端测试main函数 客户端测试main函数 编译运行结果 简述 这里介绍一下Linux进程间通信的socket方式---Loc ...
- synchronized 锁的原理
synchronized 的基本认识 在多线程并发编程中 synchronized 一直是元老级角色,很 多人都会称呼它为重量级锁.但是,随着 Java SE 1.6 对 synchronized 进 ...
- spring data jpa 代码生成!!(精华帖)
通过数据库动态生成自己想要生成的各种模板,需要了解grovery. view -> Tool Windows -> Database + -> Data source -> M ...
- Paper English
论文中的英语 单词 a arange 整理 ambiguity 含糊的 aggregate 总量 auxiliary 辅助的 alleviate 缓解 aberrant 异常的 akin 类似的 Ac ...
- 笑了,面试官问我知不知道异步编程的Future。
荒腔走板 大家好,我是 why,欢迎来到我连续周更优质原创文章的第 60 篇. 老规矩,先来一个简短的荒腔走板,给冰冷的技术文注入一丝色彩. 上面这图是我五年前,在学校宿舍拍的. 前几天由于有点事情, ...
- spring时遇到的小问题
最近在学习spring的时候遇到了两个小问题,在此总结一下 1.少导了所需要的包 运行测试程序,报出以下错误. 初步分析,得知是dataSource数据源没有创建成功,以为dataSource配置文件 ...
- 神经网络实现fashion数据集
import tensorflow as tf import numpy as np fashion=tf.keras.datasets.fashion_mnist (x_train,y_train) ...
- 小程序3:ATM小程序
ATM实现流程 1.欢迎界面和菜单 显示部分的代码代码放到view.py中 import time class View(): def login(): """ 管理员登 ...
- 如何解决Mybatis 日期查询时后面带.0
关于如何解决Mybatis 日期查询时后面带.0 MySQL数据库: 直接在MySQL查询语句中解决: SELECT T.ID, T.USERNAME, DATE_FORMAT(t.birthda ...