地址: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. 漫游Kafka设计篇之性能优化(7)

    Kafka在提高效率方面做了很大努力.Kafka的一个主要使用场景是处理网站活动日志,吞吐量是非常大的,每个页面都会产生好多次写操作.读方面,假设每个消息只被消费一次,读的量的也是很大的,Kafka也 ...

  2. Linux命令之乐--time

    time用于统计命令执行花费的总时间 例一: [root@Director usr]# time ls bin etc games include java lib lib64 libexec loc ...

  3. Codeforces Round #359 (Div. 2) C. Robbers' watch 搜索

    题目链接:http://codeforces.com/contest/686/problem/C题目大意:给你两个十进制的数n和m,选一个范围在[0,n)的整数a,选一个范围在[0,m)的整数b,要求 ...

  4. std::thread(2)

    个线程都有一个唯一的 ID 以识别不同的线程,std:thread 类有一个 get_id() 方法返回对应线程的唯一编号,你可以通过 std::this_thread 来访问当前线程实例,下面的例子 ...

  5. JAVA基础之multipart,urlencoded以及JSON

    一.(enctype) 表单的默认编码方式  ajpplication/x-www-form-urlencoded 上传文件的编码方式  multipart/form-data 互联网应用常用编码   ...

  6. centos7常用命令集合

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   CentOS7 常用命令集合 这两天一直在对CentOS 7.2进行初体验,各种学习命令肿么用,不过其实大多和DOS是一 ...

  7. MongoDB 聚合结果大小限制

    The aggregate command can return either a cursor or store the results in a collection. When returnin ...

  8. 苏宁易购微信端 全页通过background单图

    w单图,绕开了显示的兼容性. http://res.m.suning.com/project/JoinGo/intro.html http://res.m.suning.com/project/Joi ...

  9. linux下安装JDK,及配置环境变量

    首先去官网https://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新的JDK版本: 以下操作在root用户下操作 第 ...

  10. Safe Or Unsafe--hdu2527(哈夫曼树求WPL)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2527 用优先队列模拟 #include<iostream> #include<std ...