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

/***********************

简单数学题,求连续n个数的最小公倍数

**************************/

Code:

#include <iostream>
#include<string.h>
using namespace std;
int a[10000];
int gcd(int a,int b)//求最大公约数
{
int temp;
if(a<b)
{
temp = a;a = b;b = temp;
}
return (b==0)?a:gcd(b,a%b);
}
int LCM(int a,int b)//求最小公倍数
{
return a/gcd(a,b)*b; //开始写成 a*b/gcd(a,b),WA了两次,因为可能会溢出
}
int main()
{
int T,n,ans,x;
cin>>T;
while(T--)
{
//memset(a,0,sizeof(a));
ans = 1;
cin>>n>>x;
ans = LCM(ans,x);
for(int i = 0;i<n-1;i++)
{
cin>>x;
ans = LCM(ans,x);
}
cout<<ans<<endl;
} return 0;
}

ACM hdu 1019 Least Common Multiple的更多相关文章

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

  2. HDU - 1019 - Least Common Multiple - 质因数分解

    http://acm.hdu.edu.cn/showproblem.php?pid=1019 LCM即各数各质因数的最大值,搞个map乱弄一下就可以了. #include<bits/stdc++ ...

  3. HDU 1019 Least Common Multiple 数学题解

    求一组数据的最小公倍数. 先求公约数在求公倍数.利用公倍数,连续求全部数的公倍数就能够了. #include <stdio.h> int GCD(int a, int b) { retur ...

  4. HDU 1019 Least Common Multiple GCD

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

  5. 背包系列练习及总结(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 ...

  6. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

  7. HDU 4913 Least common multiple

    题目:Least common multiple 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4913 题意:有一个集合s,包含x1,x2,...,xn, ...

  8. HDU 3092 Least common multiple 01背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3092 Least common multiple Time Limit: 2000/1000 MS ...

  9. 杭电1019 Least Common Multiple【求最小公倍数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...

随机推荐

  1. yii 中设置提示成功信息,错误提示信息,警告信息

    方法一: <?php Yii::app()->user->setFlash(‘success’,”Data saved!”); 设置键值名为success的临时信息.在getFlas ...

  2. Hadoop版Helloworld之wordcount运行示例

    1.编写一个统计单词数量的java程序,并命名为wordcount.java,代码如下: import java.io.IOException; import java.util.StringToke ...

  3. 什么是jetty

    转自:http://www.cnblogs.com/eafy/archive/2007/10/24/906792.html 1. 简介 Jetty 是一个开源的servlet容器,它为基于Java的w ...

  4. 安装windows7、windows8.1提示无法创建新的分区

    有时候用原版系统镜像安装windows系统时,会提示“windows无法安装到这个磁盘.选中的磁盘采用GPT分区形式”,导致安装失败,下面就来讲解一下如何解决. 1.在系统提示无法安装的那一步,按住“ ...

  5. sort()排序 collections.sort();

    1.main方法: public class Test { public static void main(String[] args) { /** * * sort()方法详解 * 1.Collec ...

  6. myeclipse 编码问题

    在使用eclipse+MyEclipse开发中,许多文件编码默认是ISO-8859-1,不支持中文(如常用的JSP),这样我们每次建文件都要手动改编码,其实我们可以在设置文件默认编码,今后再创建时就不 ...

  7. Linux自动化运维部署+运维

    自动化部署及配置(Cobbler/Kickstart) 红帽发布的网络安装服务器套件 Cobbler可以说是一大Linux装机利器,可以快速的建立网络安装环境,据说比Kickstart还要好用. 分布 ...

  8. HTML+CSS基础学习笔记(1)

    一.了解HTML.CSS.JS 1.HTML是网页内容的载体. 内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字.图片.视频等. 2.CSS样式是表现. 用来改变内容外观的东西称之为表现 ...

  9. HTML5的你应该记住的一些知识点

    刚开始学HTML5是从w3school开始的,那只是非常简单的一些了解,后面开始看一些xiongdilian的HTML5+CSS3的视频,照着视频做了一些简单的demo(需要的童鞋可以联系我,当然网上 ...

  10. grunt 构建工具(build tool)初体验

    操作环境:win8 系统,建议使用 git bash (window下的命令行工具) 1,安装node.js 官网下载:https://nodejs.org/  直接点击install ,会根据你的操 ...