题目链接: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. node对称加密(转载)

    https://www.cnblogs.com/laogai/p/4664917.html

  2. Linux的环境配置文件----.bashrc文件

    .bashrc文件主要保存个人的一些个性化设置,如命令别名.路径等.也即在同一个服务器上,只对某个用户的个性化设置相关.它是一个隐藏文件,需要使用ls -a来查看. .bash_history   记 ...

  3. 51nod1596 搬货物

    现在有n个货物,第i个货物的重量是 2wi .每次搬的时候要求货物重量的总和是一个2的幂.问最少要搬几次能把所有的货物搬完. 样例解释: 1,1,2作为一组. 3,3作为一组. Input 单组测试数 ...

  4. VMware无法安装故障总结

    暂时遇到如下问题,并按此步骤可以解决,后续如有其它相关问题将继续更新: 1.无法获得 VMCI 驱动程序的版本: 句柄无效.如下图: 找到.vmx后缀配置文件 将如下白标""TRU ...

  5. BZOJ 3329 Xorequ (数位DP、矩阵乘法)

    手动博客搬家: 本文发表于20181105 23:18:54, 原地址https://blog.csdn.net/suncongbo/article/details/83758728 题目链接 htt ...

  6. mysql优化 explain index

    本文章属于转载,尊重原创:http://www.2cto.com/database/201501/369135.html 实验环境: 1.sql工具:Navicat 2.sql数据库,使用openst ...

  7. axios 全攻略之基本介绍与使用(GET 与 POST)

    axios axios 是一个基于 Promise 的 HTTP 客户端,专门为浏览器和 node.js 服务 Vue 2.0 官方推荐使用 axios 来代替原来的 Vue request,所以这里 ...

  8. 手动重启weblogic脚本

    手动重启weblogic脚本 pid=`ps -ef|grep fzjc_Admin_Server|grep -v grep|awk '{print $2}'` echo $pid kill -9 $ ...

  9. [WPF]c#调用默认浏览器打开网址

    //调用系统默认的浏览器 System.Diagnostics.Process.Start("http://www.zhaokeli.com");

  10. logo切图大小相应的尺寸

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTQwNDYxMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...