题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019

解题思路:lcm(a,b)=a*b/gcd(a,b)

反思:最开始提交的时候WA,以为是溢出了,于是改成了long long,还是WA,于是就不明白了,于是就去看了discuss,发现应该这样来写

lcm(a,b)=a*gcd(a,b)*b;是为了以防a乘以b太大溢出,注意啊!!!!所以就先除再乘。

#include<stdio.h>
int gcd(int a,int b)
{
int t,r;
if(a<b)
{
t=a;
a=b;
b=t;
}
r=a%b;
while(r!=0)
{ a=b;
b=r;
r=a%b;
}
return b;
} int main()
{
int ncase;
int n;
int a;
scanf("%d",&ncase); while(ncase--)
{
long s=1;
scanf("%d",&n);
while(n--)
{
scanf("%d",&a);
s=s/gcd(s,a)*a;
}
printf("%ld\n",s);
} }

  

杭电1019 Least Common Multiple【求最小公倍数】的更多相关文章

  1. HDU - 1019-Least Common Multiple(求最小公倍数(gcd))

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

  2. 杭电 2028 ( Lowest Common Multiple Plus )

    链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2028 题目要求:就是求最大公倍数,我百度了一下,最好实现的算法就是:       公式法 由于 ...

  3. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  4. (杭电1019 最小公倍数) Least Common Multiple

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  5. HDOJ 1019 Least Common Multiple(最小公倍数问题)

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

  6. 题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)

    题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  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. 1019 Least Common Multiple

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

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

随机推荐

  1. eas之编码规则&单据转换规则

    *当在企业建模中没有要显示的项目的话,则从包更新到系统树然后选择到规则定义,对申请单新增规则. 企业建模--业务规则-规则定义组织优先  多组织有先  集团优先固定值 显示格式PUR ..系统日期 2 ...

  2. Java中==和equals()的区别

  3. Java设计模式之 — 模板方法(Template Method)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744002 今天你还是像往常一样来上班,一如既往地开始了你的编程工作. 项目经理告 ...

  4. 3..jquery的ajax获取form表单数据

    jq是对dom进行的再次封装.是一个js库,极大简化了js使用 jquery库在js文件中,包含了所有jquery函数,引用:<script src="jquery-1.11.1.mi ...

  5. POJ 2914

    无向图全局最小割算法 求 G=(V, E)中任意 s-t 最小割的算法: 定义w(A, x) = ∑w(v[i], x),v[i]  A ∈  定义 Ax 为在x 前加入 A 的所有点的集合(不包括 ...

  6. Tomcat扩展——监控

    (转过来,源地址:http://www.jmatrix.org/notes/1067.html) 近期心血来潮.想能否够通过添加一个tomcat的扩展,来持续收集tomcatserver本身的性能信息 ...

  7. python中获取当前路径【os模块】

    本机windows,文件目录F:\python\ClStudyDemo\osTest.py os.path.realpath(_file_)——返回真实路径 os.path.split()——返回路径 ...

  8. luogu1315 观光公交

    题目大意 一个公交车在一条线上从1站.2站走到n站,站站间有距离.另有m个乘客在时间点t在a站等候要到b站.对于一个站,如果车比人早到,则车等人,如果人比车早到,则人等车(我一开始做题时把这个情况当作 ...

  9. h5-8 canvas

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Linux下开启vim高亮

    默认是不高亮的. [root@local ~]# vi ~/.vimrc 没有则新建这个文件. 或者修改 [root@local vim74]# vi /etc/vimrc 添加一行. syntax ...