ACM hdu 1019 Least Common Multiple
in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.
2
3 5 7 15
6 4 10296 936 1287 792 1
105
10296
/***********************
简单数学题,求连续n个数的最小公倍数
**************************/
Code:
#include <iostream>
#include<string.h>
using namespace std;
int a[10000];
int gcd(int a,int b)//求最大公约数
{
int temp;
if(a<b)
{
temp = a;a = b;b = temp;
}
return (b==0)?a:gcd(b,a%b);
}
int LCM(int a,int b)//求最小公倍数
{
return a/gcd(a,b)*b; //开始写成 a*b/gcd(a,b),WA了两次,因为可能会溢出
}
int main()
{
int T,n,ans,x;
cin>>T;
while(T--)
{
//memset(a,0,sizeof(a));
ans = 1;
cin>>n>>x;
ans = LCM(ans,x);
for(int i = 0;i<n-1;i++)
{
cin>>x;
ans = LCM(ans,x);
}
cout<<ans<<endl;
} return 0;
}
ACM hdu 1019 Least Common Multiple的更多相关文章
- 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 - 1019 - Least Common Multiple - 质因数分解
http://acm.hdu.edu.cn/showproblem.php?pid=1019 LCM即各数各质因数的最大值,搞个map乱弄一下就可以了. #include<bits/stdc++ ...
- HDU 1019 Least Common Multiple 数学题解
求一组数据的最小公倍数. 先求公约数在求公倍数.利用公倍数,连续求全部数的公倍数就能够了. #include <stdio.h> int GCD(int a, int b) { retur ...
- HDU 1019 Least Common Multiple GCD
解题报告:求多个数的最小公倍数,其实还是一样,只需要一个一个求就行了,先将答案初始化为1,然后让这个数依次跟其他的每个数进行求最小公倍数,最后求出来的就是所有的数的最小公倍数.也就是多次GCD. #i ...
- 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)
作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...
- ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)
Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...
- HDU 4913 Least common multiple
题目:Least common multiple 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4913 题意:有一个集合s,包含x1,x2,...,xn, ...
- HDU 3092 Least common multiple 01背包
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3092 Least common multiple Time Limit: 2000/1000 MS ...
- 杭电1019 Least Common Multiple【求最小公倍数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...
随机推荐
- JavaScript---网络编程(12)--DHTML技术演示(5)-form表单验证技术(正则)
这里不进行很复杂的后台验证以及JavaScript的正则表达式,只是简单的介绍下这个技术,简单的后台接收与跳转,大概了解怎么验证的就可以.具体的技术,我后面还会继续写博客的.本人也还在学习中. 表单验 ...
- codeforces Codeforces 650A Watchmen
题意:两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离 也就是:x1=x2或y1=y2,再删除重合点造成的重复计数即可. #include <stdio.h> #includ ...
- Lambda表达式与匿名方法
在C#2中,由于有了方法组,匿名方法,类型的协变和抗变,使得运用delegate变得很容易,在注册事件时代码变得简单易读,但是在C# 2中,代码仍然有点臃肿,大块的匿名方法会降低代码的可读性,一般我们 ...
- Command-line interface
A command-line interface (CLI), also known as command-line user interface, console user interface, a ...
- js 日期插件 datepicker
点击图片出现 时间 ,增加一个点击事件 <label for="" class="width80">创建日:</label> < ...
- 深入了解Angularjs指令中的ngModel
关于AngularJs的指令的知识学习,请参考... 这次我们接上次没讲完的知识继续. 前端人员在设计表单逻辑时, 在大部分情况下,我们需要为表单定义很多指令, 比如比较两个input内的值是否相同, ...
- java代理
首先,java中为什么要提出代理的设计模式呢?代理模式的作用是:为其它对象提供一种代理以控制对这个对象的訪问.在某些情况下,一个客户不想或者不能直接引用还有一个对象,而代理对象能够在client和目标 ...
- dig命令 安装
获取容器 dns 信息 需要安装dig 命令 yum install bind-utils
- myeclipse开发代码颜色搭配保护视力
废话不多说,这个东西主要是为了保护视力的,另外我也挺喜欢上边的颜色搭配的,今天特拿出来分享.直接上图
- [Node.js] Exporting Modules in Node
In this lesson, you will learn the difference between the exports statement and module.exports. Two ...