HDU1019 Least Common Multiple(多个数的最小公倍数)
InputInput 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.
OutputFor 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 这道题挺简单的,但我居然想着去分解质因数然后乘一遍,又果断的TLE了.但其实真的很简单,不断地读入一个数,然后求它和之前所有数的lcm,这个怎么求呢?除去两数的gcd即可.说起来还是太菜了.... 代码如下:
#include<cstdio>
using namespace std; long long ans=,n,i,x; long long gcd(long long a,long long b)
{
if(b>a)
{
long long w=a;a=b;b=w;
}
if(a%b==)
{
return b;
}
else
{
return gcd(b,a%b);
}
} int main()
{
long long t;
scanf("%lld",&t);
while(t--)
{
ans=;
scanf("%lld",&n);
for(i=;i<=n;i++)
{
scanf("%lld",&x);
ans*=x/gcd(ans,x);
}
printf("%lld\n",ans);
}
return ;
}
每日刷题身体棒棒!
HDU1019 Least Common Multiple(多个数的最小公倍数)的更多相关文章
- [暑假集训--数论]hdu1019 Least Common Multiple
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...
- hdu1019 Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- HDU-1019 Least Common Multiple
http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/ ...
- HDU——1019Least Common Multiple(多个数的最小公倍数)
Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- hdu 2028 Lowest Common Multiple Plus(最小公倍数)
Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- ACM-简单题之Least Common Multiple——hdu1019
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- 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 ...
- hdu_1019Least Common Multiple(最小公倍数)
太简单了...题目都不想贴了 //算n个数的最小公倍数 #include<cstdio> #include<cstring> #include<algorithm> ...
- 最大公约数最小公倍数 (例:HDU2028 Lowest Common Multiple Plus)
也称欧几里得算法 原理: gcd(a,b)=gcd(b,a mod b) 边界条件为 gcd(a,0)=a; 其中mod 为求余 故辗转相除法可简单的表示为: int gcd(int a, int b ...
随机推荐
- android自定义动画
前一篇说了实现过程,这次来写一个自己简单实现的3d动画 先来属性声明配置,方便使用xml 文件来定制动画 <!-- 有些类型其实是没必要的,只是实例代码,为了更具有代表性 --> < ...
- AngularJS 框架
AngularJS 通过 指令 扩展了 HTML,且通过 表达式 绑定数据到 HTML. [Angular JS表达式] 1.Angular JS使用双{{}}绑定方式.用于将表达式的内容输出到页面 ...
- .NetCore之下载文件
本篇将和大家分享的丝.NetCore下载文件,常见的下载有两种:A标签直接指向下载文件地址和post或get请求后台输出文件流的方式,本篇也将围绕这两种来分享:如果对您有好的帮助,请多多支持. 允许站 ...
- Linux入门之常用命令(4)vi编辑器
vi分为三种模式 一般模式:删除字符.删除整行.复制粘贴等操作 编辑模式:i o a r进入 输入字符 Esc退出 命令行模式::或/ 将光标移动到最末行 搜寻数据 读取或替换 退出vi 显示行号 ...
- DIY智能家居——零基础入门篇
概要 本文主要根据笔者从零开始接触硬件,以小白视角开启IoT探索,根据相关资料DIY一个温湿度传感器.后经过探索发现新大陆--Home Assistant&Homebridge,最终实现了一个 ...
- Codeforces 858A. k-rounding 数论
题目: 题意:输入n和k,找到一个最小的数,满足末尾有至少k个0和是n的倍数. 最小的情况 ans = n,最大的情况 ans = n*pow(10,k). 令 k = pow(10,k); 我们发现 ...
- JavaScript性能优化之函数节流(throttle)与函数去抖(debounce)
函数节流,简单地讲,就是让一个函数无法在很短的时间间隔内连续调用,只有当上一次函数执行后过了你规定的时间间隔,才能进行下一次该函数的调用. 函数节流的原理挺简单的,估计大家都想到了,那就是定时器.当我 ...
- Python单元测试框架
目录 概况 系统要求 使用PyUnit构建自己的测试 安装 测试用例介绍 创建一个简单测试用例 复用设置代码:创建固件 包含多个测试方法的测试用例类 将测试用例聚合成测试套件 嵌套测试用例 测试代码的 ...
- zoj1151 zoj1295 Word Reversal 字符串的简单处理
Word Reversal Time Limit: 2 Seconds Memory Limit:65536 KB For each list of words, output a line ...
- vue + ajax + php 接口的使用小接
vue + ajax + php 接口的使用小接 前端代码: (获取用户信息,并渲染页面) userinfor.html <!DOCTYPE html> <html lang=&qu ...