Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】
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", ×);
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】【欧几里得辗转相除法】的更多相关文章
- interesting Integers(数学暴力||数论扩展欧几里得)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8
- hdu 1573 A/B (扩展欧几里得)
Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). Input 数据的第一行 ...
- [ZLXOI2015]殉国 数论 扩展欧几里得
题目大意:已知a,b,c,求满足ax+by=c (x>=0,y>=0)的(x+y)最大值与最小值与解的个数. 直接exgcd,求出x,y分别为最小正整数的解,然后一算就出来啦 #inclu ...
- 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 ...
- 数论 + 扩展欧几里得 - 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< ...
- HDU 1222 Wolf and Rabbit(欧几里得)
Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 数论--扩展欧几里得exgcd
算法思想 我们想求得一组\(x,y\)使得 \(ax+by = \gcd(a,b)\) 根据 \(\gcd(a,b) = \gcd(b,a\bmod b)\) 如果我们现在有\(x',y'\) 使得 ...
- <数论相关>欧几里得与拓展欧几里得证明及应用
欧几里得算法 欧几里得算法的复杂度为O(log(n)),是一个非常高效的求最大公约数算法. 在这里不证明欧几里得算法的复杂度,有兴趣的可以访问以下链接:http://blog.sina.com.cn/ ...
- 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 ...
随机推荐
- 基于titanic数据集预测titanic号旅客生还率
数据清洗及可视化 实验内容 数据清洗是数据分析中非常重要的一部分,也最繁琐,做好这一步需要大量的经验和耐心.这门课程中,我将和大家一起,一步步完成这项工作.大家可以从这门课程中学习数据清洗的基本思路以 ...
- Andriod安卓下开发UHF读写器
随着在Andriod设备上使用UHF读写器变得越来越广泛,友我科技独立研发了UHF读写器的android开发包,使用此开发包,工程师只需在工程中导入jar包,使用java语言就可以轻松的开发出Andr ...
- Tomcat乱码或异常
一.控制台乱码 原因:Tomcat与Windows编码不一致导致 解决办法:首先找到conf/logging.properties文件,然后打开后找到“java.util.logging.Consol ...
- 服务器性能测试实时监控Linux命令
实时监控 top -h : 查看帮助 -p : 监控指定进程,当监控多个进程时,进程ID以逗号隔开,这个选项只能在命令行下使用 top 任务区命令 M: 按内存使用率排序(大写) P:按CPU使用率排 ...
- Django中获取参数(路径,查询,请求头,请求体)
一.通常HTTP协议向服务器传参有几种途径 : 提取URL的特定部分,如/weather/shanghai/2018,可以在服务器端的路由中用正则表达式截取: 查询字符串(query string), ...
- java之Objects类
Objects类概述 在JDK7添加了一个Objects工具类,它提供了一些方法来操作对象,它由一些静态的实用方法组成,这些方法是null-save(空指针安全的)或null-tolerant(容忍空 ...
- 【洛谷5369】[PKUSC2018] 最大前缀和(状压DP)
点此看题面 大致题意: 对于一个序列,求全排列下最大前缀和之和. 状压\(DP\) 考虑如果单纯按照题目中对于最大前缀和的定义,则一个序列它的最大前缀和是不唯一的. 为了方便统计,我们姑且规定,如果一 ...
- [译]Vulkan教程(24)索引buffer
[译]Vulkan教程(24)索引buffer Index buffer 索引buffer Introduction 入门 The 3D meshes you'll be rendering in a ...
- JS Proxy(代理)
前言 Proxy 也就是代理,可以帮助我们完成很多事情,例如对数据的处理,对构造函数的处理,对数据的验证,说白了,就是在我们访问对象前添加了一层拦截,可以过滤很多操作,而这些过滤,由你来定义. 想了解 ...
- 【安富莱】V6,V5开发板用户手册,重在BSP驱动包设计方法,HAL库的框架学习,授人以渔(2019-11-04)
说明: 1.本教程重在BSP驱动包设计方法和HAL库的框架学习,并将HAL库里面的各种弯弯绕捋顺,从而方便我们的程序设计. 2.本次工程延续以往的代码风格,从底层BSP驱动包到应用代码,变量命名,文件 ...