Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

标签: 入门讲座题解 数论


题目描述

 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

题意

给定几个数,求这几个数的最小公倍数。


解析

这是一道基础的欧几里得辗转相除法的题。可以使用朴素gcd算法不断求最小公倍数。


通过代码

/*
Problem
HDU - 1019
Status
Accepted
Memory
1372kB
Length
466
Lang
G++
Submitted
2019-11-25 22:13:37
Shared RemoteRunId
31637683
*/ #include <bits/stdc++.h>
using namespace std; int gcd(int a, int b)
{
return b? gcd(b, a % b): a;
} int lcm(int a, int b)
{
return a / gcd(a, b) * b; //注意此处不是 a * b / gcd,这样容易爆int.
}
int main()
{
int times;
scanf("%d", &times); while(times --){
int n;
int t1, t2; scanf("%d%d", &n, &t1); for(int i = 1; i < n; i ++){
scanf("%d", &t2); t1 = lcm(t1, t2); //不断地将前面求得的最小公倍数当作a,新输入的数当作b,继续求最小公倍数.
} printf("%d\n", t1);
} return 0;
}

Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】的更多相关文章

  1. interesting Integers(数学暴力||数论扩展欧几里得)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8

  2. hdu 1573 A/B (扩展欧几里得)

    Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). Input 数据的第一行 ...

  3. [ZLXOI2015]殉国 数论 扩展欧几里得

    题目大意:已知a,b,c,求满足ax+by=c (x>=0,y>=0)的(x+y)最大值与最小值与解的个数. 直接exgcd,求出x,y分别为最小正整数的解,然后一算就出来啦 #inclu ...

  4. SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3

    141. Jumping Joe time limit per test: 0.25 sec. memory limit per test: 4096 KB Joe is a frog who lik ...

  5. 数论 + 扩展欧几里得 - SGU 106. The equation

    The equation Problem's Link Mean: 给你7个数,a,b,c,x1,x2,y1,y2.求满足a*x+b*y=-c的解x满足x1<=x<=x2,y满足y1< ...

  6. HDU 1222 Wolf and Rabbit(欧几里得)

    Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. 数论--扩展欧几里得exgcd

    算法思想 我们想求得一组\(x,y\)使得 \(ax+by = \gcd(a,b)\) 根据 \(\gcd(a,b) = \gcd(b,a\bmod b)\) 如果我们现在有\(x',y'\) 使得 ...

  8. <数论相关>欧几里得与拓展欧几里得证明及应用

    欧几里得算法 欧几里得算法的复杂度为O(log(n)),是一个非常高效的求最大公约数算法. 在这里不证明欧几里得算法的复杂度,有兴趣的可以访问以下链接:http://blog.sina.com.cn/ ...

  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. CSRF漏洞原理浅谈

    CSRF漏洞原理浅谈 By : Mirror王宇阳 E-mail : mirrorwangyuyang@gmail.com 笔者并未深挖过CSRF,内容居多是参考<Web安全深度剖析>.& ...

  2. Android极致优化

    1.SVG 可缩放矢量图,svg不会跟位图一样因为缩放使图片质量下降,有点在于节约空间与内存,常用语简单的小图标,由xml定义的,根节点为<svg>,在android中通过vector实现 ...

  3. C语言入门-类型定义

    一.自定义数据类型(typedef) c语言提供一个叫做typedef的功能来声明一个已有的数据类型的新名字,比如: typedef int length; 这样length成为了int类型的别名 这 ...

  4. 【bzoj5339】[TJOI2018]教科书般的亵渎(拉格朗日插值/第二类斯特林数)

    传送门 题意: 一开始有很多怪兽,每个怪兽的血量在\(1\)到\(n\)之间且各不相同,\(n\leq 10^{13}\). 然后有\(m\)种没有出现的血量,\(m\leq 50\). 现在有个人可 ...

  5. 【模板】分治 FFT

    Link Solution 有两种解法. 法1: 直接上分治FFT,也就是CDQ分治+FFT. 具体做法是先递归左半边,算出左半边答案之后,将左半边贡献到右半边,然后递归右半边. 分治是一个log的, ...

  6. 菜鸟刷面试题(三、Redis篇)

    目录: redis是什么?都有哪些使用场景? redis有哪些功能? redis和memecache有什么区别? redis为什么是单线程的? 什么是缓存穿透?怎么解决? redis支持的数据类型有哪 ...

  7. TYUT程序设计入门第四讲练习题题解--数论入门

    程序设计入门第四讲练习题题解--数论入门 对于新知识点的学习,需要不断地刷题训练,才能有所收获,才能更好地消化知识点. 题组链接: 程序设计入门第四讲练习题--数论 by vjudge 题解: A. ...

  8. CSharpGL(56)[译]Vulkan入门

    CSharpGL(56)[译]Vulkan入门 本文是对(http://ogldev.atspace.co.uk/www/tutorial50/tutorial50.html)的翻译,作为学习Vulk ...

  9. sql server报【将截断字符串或二进制数据】错误

    会出现这个错误的原因是因为表设置的列长度小于要插入的数据的长度. 可以从下列的6个方面去排查: 1.表设置的列名长度太短. 2.插入的数据太长. 3.有默认值. 4.有触发器. 5 从char数据类型 ...

  10. Vue 从入门到进阶之路(十一)

    之前的文章我们说了一下 vue 中组件的原生事件绑定,本章我们来所以下 vue 中的插槽使用. <!DOCTYPE html> <html lang="en"&g ...