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. C# ling to sql 取多条记录最大时间

    var _setList = (from f in _postgreDbContext.settlements group f by ( new { f.settlement_code })into ...

  2. js-08-数组学习

    一.数组语法格式 var name=[item1,item2,......] 二.数组的声明创建 var arr=new Aarray( ) //声明一个空数组对象 var arr=new Array ...

  3. SAP QM 检验批里某检验特性的取样数量跟检验计划设置不符?

    SAP QM 检验批里某检验特性的取样数量跟检验计划设置不符? 如下检验批号 890000045939, 结果录入界面,第二个特性里,取样数量是50个.实际上,该检验批对应的检验计划里, 采样过程L- ...

  4. Computer: Use the mouse to open the analog keyboard

    Xx_Introduction Please protection,respect,love,"China's Internet Security Act"! For learni ...

  5. Cesium专栏-卫星轨迹

    Cesium Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精 ...

  6. Android8.1 SystemUI源码分析之 Notification流程

    代码流程 1.先看UI显示,StatuBar加载 CollapsedStatusBarFragment 替换 status_bar_container(状态栏通知显示区域) SystemUI\src\ ...

  7. STM32 HAL_Deleay() 函数 导致程序卡死

    出现问题场景:   我的程序有RTOS操作系统.使用的驱动库是STM32官方最新的HAL库. 移植好LwIP以太网协议后,在初始化网卡阶段程序卡死.   出现问题原因:   后经过蠢笨的printf打 ...

  8. Vue+Vuex初体验

    首先: 安装vuex npm install vuex -S 需要有两个组件(HelloWord.vue 和 HelloDemo.vue)[组件自定义] 注册路由 注册store 测试 一.需要有两个 ...

  9. SQL Server事务复制(sql 2008 r2)

    一.环境准备 1.两个虚拟服务器 主机1:XINXIBU01  作为发布和分发服务器   主 机2:XINXIBU02 192.168.1.160  作业阅服务器 2.SQL SERVER sql 2 ...

  10. UDP组播

    多播(组播) 组播组可以是永久的也可以是临时的.组播组地址中,有一部分由官方分配的,称为永久组播组.永久组播组保持不变的是它的ip地址,组中的成员构成可以发生变化.永久组播组中成员的数量都可以是任意的 ...