题目链接: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. 6.2 C# 2:利用 yield 语句简化迭代器

    class Program { static void Main(string[] args) { object[] values = new object[] { "a", &q ...

  2. 表操作(day03)

    回顾: 1.单行函数 2.表连接 oracle中的表连接 内连接 等值连接 select e.id,e.first_name,d.name from s_emp e,s_dept d where e. ...

  3. 09.正则表达式re-2.complie函数

    compile 函数用于编译正则表达式,生成一个 Pattern 对象,它的一般使用形式如下: import re # 将正则表达式编译成 Pattern 对象 pattern = re.compil ...

  4. 09.正则表达式re-1.正则表达式

    1.正则表达式概述 正则表达式(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的一个概念. 正则表达式使用单个字符串来描述.匹配一系列匹配某 ...

  5. canvas实现圆框图片

    作者:issac_宝华链接:http://www.jianshu.com/p/9a6ee2648d6f來源:简书 在html中做圆框图片很容易,只需要简单的 border-radius: 50%; 当 ...

  6. cxdbImage以及图像显示

    把pdf以及图像存入数据库,然后根据需要显示出来.在处理的过程中,不同类型的图像格式有其不同的类,如果这个概念不清楚,就会绕一个很大的圈子. MyJPEG : TJPEGImage ; mypng : ...

  7. jsonp 后台返回注意事项

    前端代码 <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script ...

  8. ZOJ - 3483 - Gaussian Prime

    先上题目: Gaussian Prime Time Limit: 3 Seconds      Memory Limit: 65536 KB In number theory, a Gaussian ...

  9. PHP扩展开发-测验成功

    原文:http://kimi.it/496.html http://blog.csdn.net/u011957758/article/details/72234075 ---------------- ...

  10. 能够在子线程绘画的View SurfaceView

    转载请注明出处:王亟亟的大牛之路 近期两天都没有写文章,一方面是自己在看书.一方面不知道写什么,本来昨天想写Glide或者RxAndroid的东西结果公司的"狗屎"网怎么都刷不好G ...