地址:http://acm.hdu.edu.cn/showproblem.php?pid=1019

题目:

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
 
思路:最小公倍数(lcm),最大公约数(gcd)。
  求gcd(a,b),用辗转相除法。
  代码:

long gcd(long a,long b)
{
long r=;
while
(r>)
{

r=a%b;
a=b;
b=r;
}

return
a;}
lcm(a,b)=a*b/gcd(a,b);
代码:
  
long lcm(long a,long b)
{

return
a/gcd(a,b)*b;
}

ac代码:
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std; long gcd(long a,long b)
{ long r=;
while (r>)
{
r=a%b;
a=b;
b=r;
}
return a;
// if(b)
// while( (a%=b) && (b %= a));
// return a+b;
} long lcm(long a,long b)
{
return a/gcd(a,b)*b;
}
int main (void)
{
int t,n;
cin>>t;
while(t--)
{
long temp,m;
cin>>n;
scanf("%ld",&temp);
for(int i = ;i<n;i++)
{
scanf("%ld",&m);
temp = lcm(temp,m);
}
cout<<temp<<endl; }
return ;
}
 

杭电1019Least Common Multiple的更多相关文章

  1. 杭电1019-Least Common Multiple

    #include<stdio.h>int gcd(int a,int b);int main(){    int n,m,a,b,i,sum;//sum是最小公倍数    scanf(&q ...

  2. HDU——1019Least Common Multiple(多个数的最小公倍数)

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

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

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

  4. 杭电 1159 Common Subsequence

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  5. 杭电1159 Common Subsequence【最长公共子序列】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 解题思路:任意先给出两个字符串 abcfbc abfcab,用dp[i][j]来记录当前最长的子 ...

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

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

  7. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  8. 杭电dp题集,附链接还有解题报告!!!!!

    Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...

  9. 2018 Multi-University Training Contest 1 杭电多校第一场

    抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001  Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...

随机推荐

  1. hdu 4722(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...

  2. Pecan

    什么是peacn Pecan是一个轻量级的基于Python的Web框架, Pecan的目标并不是要成为一个“full stack”的框架, 因此Pecan本身不支持类似Session和Database ...

  3. 【原】storm源码之一个class解决nimbus单点问题

    一.storm nimbus 单节点问题概述 1.storm集群在生产环境部署之后,通常会是如下的结构:从图中可以看出zookeeper和supervisor都是多节点,任意1个zookeeper节点 ...

  4. sql语句判断身份证性别等

    SELECT t.card_number ,) AS "省份", SUBSTR(t.card_number,,) "出生年月", SUBSTR(t.card_n ...

  5. js 闭包与垃圾回收-待删

    关于闭包请看戳 串讲-解释篇:作用域,作用域链,执行环境,变量对象,活动对象,闭包,本篇写的不太好: 先摆定义: 函数对象,可以通过作用域链相互关联起来,函数体内部的变量都可以保存在函数作用域内,这种 ...

  6. 使用response来控制浏览器的缓存

    缓存这个技术在我们实际的开发中是非常常用的,也是非常重要的一项技术.主要用于客户端(浏览器)向服务器端请求的是一些比较大的数据,并且这个数据在短时间内不会经常发生变化的情况,比如一些网站的logo图片 ...

  7. 即使关闭了nagle算法,粘包依旧存在

    JAVA高级架构 https://mp.weixin.qq.com/s?src=11&timestamp=1542107581&ver=1242&signature=OoktA ...

  8. 预见未来丨机器学习:未来十年研究热点 量子机器学习(Quantum ML) 量子计算机利用量子相干和量子纠缠等效应来处理信息

    微软研究院AI头条 https://mp.weixin.qq.com/s/SAz5eiSOLhsdz7nlSJ1xdA 预见未来丨机器学习:未来十年研究热点 机器学习组 微软研究院AI头条 昨天 编者 ...

  9. Storm-源码分析- Component ,Executor ,Task之间关系

    Component包含Executor(threads)的个数 在StormBase中的num-executors, 这对应于你写topology代码时, 为每个component指定的并发数(通过s ...

  10. C#日期处理(转) 太忘记了,备忘

    //今天 DateTime.Now.Date.ToShortDateString(); //昨天,就是今天的日期减一 DateTime.Now.AddDays(-1).ToShortDateStrin ...