传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019

Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 61592    Accepted Submission(s): 23486

Problem Description
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
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1021 1061 1049 1108 1020 
 
题目意思:
求n个数的最小公倍数
直接暴力即可:暴力规则:求第一个和第二个元素的最小公倍数,然后拿求得的最小公倍数和第三个元素求最小公倍数,继续下去,直到没有元素
小技巧:
通过最大公约数求最小公倍数的时候,先除再乘,避免溢出
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int gcd(int a,int b)
{
if (b==)
return a;
return gcd(b, a%b);
}
int main()
{
//公式:a,b的最小公倍数等于a,b的乘积除以a,b的最大公约数
//直接暴力即可
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a=,cnt=;
int x;
for(int i=;i<n;i++)
{
scanf("%d",&x);
cnt=a/gcd(a,x)*x;//先除后乘,避免溢出
a=cnt;
}
printf("%d\n",a);
}
return ;
}

HDU 1019 (多个数的最小公倍数)的更多相关文章

  1. hdu 1019 n个数的最小公倍数

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  2. hdu 1788(多个数的最小公倍数)

    Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

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

  4. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

  5. n个数的最小公倍数

    Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最小公倍数,每个测 ...

  6. HDOJ-ACM1019(JAVA) 多个数的最小公倍数

    题意:求多个数的最小公倍数 很简单,但是我一开始的做法,估计会让结果越界(超过int的最大值) import java.util.*; import java.io.*; public class M ...

  7. ACM hdu 1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  8. HDU_2028——求多个数的最小公倍数

    Problem Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最 ...

  9. HDU 1019 Least Common Multiple GCD

    解题报告:求多个数的最小公倍数,其实还是一样,只需要一个一个求就行了,先将答案初始化为1,然后让这个数依次跟其他的每个数进行求最小公倍数,最后求出来的就是所有的数的最小公倍数.也就是多次GCD. #i ...

随机推荐

  1. Python基础学习总结(七)

    9.类 面对对象编程Object Oriented Programming,简称OOP. 面向对象编程是最有效的软件编写方法之一.在面向对象编程中,你编写表示现实世界中的事物和情景的类,并基于这些类来 ...

  2. php获取今日开始时间和结束时间

    $begintime=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y'))); $endtime=date( ...

  3. 关于css伪类,伪元素详解总结

    伪类 伪类就是一种虚构的状态或者说是一个具有特殊属性的元素可以使用CSS进行样式修饰.常见的几种伪类是: :link , :visited , :hover , :active , :first-ch ...

  4. WHILE (Transact-SQL)

    ---循环 declare @n int declare @rowcount int declare @name varchar(50) create table #temp ( id int ide ...

  5. 申请微信小程序步骤

    一.注册 注册网址:https://mp.weixin.qq.com/ 选择账号类型:选择 小程序 注册账号 填写邮箱密码并激活:未注册过公众平台.开放平台.企业号.未绑定个人号的邮箱. 填写主体信息 ...

  6. WINDOWS API ——CREATETOOLHELP32SNAPSHOT——查找进程

    原文:http://www.cnblogs.com/wind-net/archive/2012/10/26/2741458.html //根据进程名获取进程ID DWORD GetPidByProce ...

  7. Visual Studio Code调试electron主进程

    Visual Studio Code调试electron主进程 作者: jekkay 分类: electron 发布时间: 2017-06-11 14:56  一·概述 此文原出自[水滴石]: htt ...

  8. Spring3实战第二章第二小节 IOC依赖注入 list和map集合

    Spring有多种依赖注入的形式,本篇文章仅介绍Spring通过xml进行IOC配置的方式. 1.Set注入 2.构造器注入 平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new ...

  9. metasploit 连接database相关问题

    我们首先去这个目录下看database.yml文件内容: 下图是我们看到的的信息 接着打开metasploit,运行db_connect 指令链接数据库.格式为: db_connect 用户名:密码@ ...

  10. PHP discuz模板语法

    Discuz! X 模板的解析主要是 ./source/class/class_template.php 文件解析处理的,如果需要深入了解请可以看看这个文件! 模板嵌套语法 将被嵌套模板内容解析为 P ...