Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42735    Accepted Submission(s): 16055

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=1的情况,按我之前先取两个数得到第一个公倍数的做法会超时,n=1根本没无法输出。因此要重新写,顺便复习下gcd公式
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
long long gcd(long long a,long long b)
{
return b?gcd(b,a%b):a;//还是记这个吧,简单易用
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n;
long long a,tlcm,beg=1;//让beg初始化为1不影响结果并成为第0个数,这样一开始也就可以一个一个地求gcd
scanf("%d",&n);
for (int i=0; i<n; i++)
{
scanf("%lld",&a);
beg=(beg*a)/gcd(a,beg);
}
cout<<beg<<endl;
}
return 0;
}

HDU——1019Least 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. HDU1019 Least Common Multiple(多个数的最小公倍数)

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

  3. 杭电1019Least Common Multiple

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1019 题目: Problem Description The least common multiple ...

  4. 杭电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 ...

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

  6. hdu 2028 Lowest Common Multiple Plus(最小公倍数)

    Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  7. hdu_1019Least Common Multiple(最小公倍数)

    太简单了...题目都不想贴了 //算n个数的最小公倍数 #include<cstdio> #include<cstring> #include<algorithm> ...

  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. HDU 1019 (多个数的最小公倍数)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (J ...

随机推荐

  1. js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了

    js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了

  2. 禁止DataGridView控件中添加和删除行

    实现效果: 知识运用: DataGridView控件的AllowUserToAddRows AllowUserDeleteRows和ReadOnly属性 实现代码: private void btn_ ...

  3. 在2d游戏中常用的向量方式

    function cc.exports.VectorRotateByAngle(vector,angle)--计算向量旋转后的向量,angle:正数逆时针,负输顺时针 angle = angle*ma ...

  4. cocos2x (c++/lua) spine 文件的预加载

    在之前,笔者写过一编博客,通过lua在加载场景加载spineAnimation动画精灵,保存在table中,然后在游戏中创建动画精灵时,提取加载好的spineAnimaiton中的 spSkeleto ...

  5. Element表单验证(2)

    Element表单验证(2) 上篇讲的是async-validator的基本要素,那么,如何使用到Element中以及怎样优雅地使用,就在本篇. 上篇讲到async-validator由3大部分组成 ...

  6. NOIP模拟赛 czy的后宫4

    czy的后宫4 [问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到- ...

  7. 如何用纯 CSS 创作一个方块旋转动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gjgyWm 可交互视频 ...

  8. python数据类型之字典(dict)和其常用方法

    字典的特征: key-value结构key必须可hash,且必须为不可变数据类型.必须唯一. # hash值都是数字,可以用类似于2分法(但比2分法厉害的多的方法)找.可存放任意多个值.可修改.可以不 ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分

    题目:题目链接 思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + ...

  10. 安装repo

    $ sudo apt-get install curl -y$ curl "http://android.git.linaro.org/gitweb?p=tools/repo.git;a=b ...