地址: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. jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表

    对于问题从后台获取json数据,将内容填充到下拉列表,代码非常简单,具体过程请看下面代码. 需求:url:链接     par:ID       sel:下拉列表选择器 function BuildS ...

  2. Windows网络接口API函数

    Windows提供了一套非常轻量级的网络函数,方便进行网络应用开发,整理出来供参考使用. The following functions are used in Windows networking: ...

  3. 第三篇:C++ 中的几种初始化

    前言 阅读C++教材时,想必你听过复制初始化,直接初始化,值初始化这三个概念吧.笔者本人常将其混淆,遂在此记录下它们的具体含义以便日后查阅. 复制初始化( copy-initialization ) ...

  4. 用一个二维码做下载地址,自动区分是 ios 还是 android

    用一个二维码做下载地址,自动区分是 ios 还是 android, 甚至区分 iphone  和 ipad. <html> <head> <meta http-equiv ...

  5. cobbler上部署centos系统修改网卡地址成eth0

    编辑cobbler的profile文件:   cobbler profile edit --name=CentOS-7.2-x86_64 --kopts='net.ifnames=0 biosdevn ...

  6. 【转】Spring Boot 日志配置(超详细)

    更新日志: 20170810 更新通过 application.yml传递参数到 logback 中. [toc] 简书不支持目录,截图一张. image.png 默认日志 Logback: 默认情况 ...

  7. OKEx量化分析报告[2017-12-19]

    [分析时间]2017-12-19 09:05 [分析对象]OKEx [有效期限]2017-12-19 09:00:00 — 2017-12-19 09:59:59 [报告内容]DASH_USDT   ...

  8. The Highest Mark---hdu5501(问题转化 01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5501 二维数组: #include<stdio.h> #include<iostre ...

  9. Redis应用案例,查找某个值的范围(转)

    原文:https://groups.google.com/forum/#!topic/redis-db/lrYbkbxfQiQ 本文来自Redis在Google Group上的一个问题,有一位同学发贴 ...

  10. LInux进程虚拟地址空间的管理

    2017-04-07 脱离物理内存的管理,今天咱们来聊聊进程虚拟内存的管理.因为进程直接分配和使用的都是虚拟内存,而物理内存则是有系统“按需”分配给进程,在进程看来,只知道虚拟内存的存在! 前言: 关 ...