zoj1797 Least Common Multiple 最小公倍数
Least Common Multiple
Time Limit:
2 Seconds Memory Limit:
65536 KB
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,拿出来的唯一原因是::注意int是否会超。
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
int _gcd(int a,int b)
{
if(b==0) return a;
return _gcd(b,a%b);
}
int _solve(int v,int u)
{
int m=_gcd(u,v);
return u/m*v;//这里一定要先除,不然会超。。。。。qwq
}
int main()
{
int T,n,ans,x;
cin>>T;
while(T--)
{
cin>>n;
cin>>ans;
for(int i=2;i<=n;i++)
{
cin>>x;
ans=_solve(ans,x);
}
cout<<ans<<endl;
} return 0;
}
zoj1797 Least Common Multiple 最小公倍数的更多相关文章
- HDOJ 1019 Least Common Multiple(最小公倍数问题)
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- hdu_1019Least Common Multiple(最小公倍数)
太简单了...题目都不想贴了 //算n个数的最小公倍数 #include<cstdio> #include<cstring> #include<algorithm> ...
- Least Common Multiple (最小公倍数,先除再乘)
思路: 求第一个和第二个元素的最小公倍数,然后拿求得的最小公倍数和第三个元素求最小公倍数,继续下去,直到没有元素 注意:通过最大公约数求最小公倍数的时候,先除再乘,避免溢出 #include ...
- hdu 2028 Lowest Common Multiple Plus(最小公倍数)
Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 最大公约数最小公倍数 (例:HDU2028 Lowest Common Multiple Plus)
也称欧几里得算法 原理: gcd(a,b)=gcd(b,a mod b) 边界条件为 gcd(a,0)=a; 其中mod 为求余 故辗转相除法可简单的表示为: int gcd(int a, int b ...
- HDU - 1019-Least Common Multiple(求最小公倍数(gcd))
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...
- HDU1019 Least Common Multiple(多个数的最小公倍数)
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...
- 题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)
题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- (杭电1019 最小公倍数) Least Common Multiple
Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- Oracle之range,hash,list分区现实应用及优缺点汇总
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp51 [align=center;] Oracle之range,hash,l ...
- 转:【Java并发编程】之二十二:并发新特性—障碍器CyclicBarrier(含代码)
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17512983 CyclicBarrier(又叫障碍器)同样是Java5中加入的新特性,使用 ...
- 结对作业--基于GUI的四则运算生成器
组员:201421123015 陈麟凤 201421123019 张志杰 201421123020 黄海鸿 coding 地址 :https://coding.net/u/zhang1995/p/wo ...
- 团队项目汇总beta
一.Daily Scrum Meeting[Alpha] 4.23-第一天 4.24-第二天 4.25-第三天 4.26-第四天 4.27-第五天 4.28-第六天 4.29-第七天 二.Daily ...
- 201521123092《java程序设计》第三周学习总结
#1. 本章学习总结 你对于本章知识的学习总结 #2. 书面作业 **Q1. 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private ...
- 201521123013 《Java程序设计》第12周学习总结
1. 本章学习总结 2. 书面作业 将Student对象(属性:int id, String name,int age,double grade)写入文件student.data.从文件读出显示. Q ...
- 201521123023《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. (1)File可以表示一个特定文件的名称,也能表示一个目录下的一组文件的名称. (2)继承自InputStre ...
- 201521123018 《Java程序设计》第10周学习总结
1. 本章学习总结 你对于本章知识的学习总结 2. 书面作业 一.inally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? 答: 4-2中 ...
- phpcms总结(转)
转自:http://www.cnblogs.com/zuzwn/p/3649536.html文件目录结构 根目录 | – api 接口文件目录 | – caches 缓存文件目录 | – config ...
- 聊一聊我的阿里云ECS云主机
javaweb学习有段时间了,期间也编写了一些自己的小webapp应用,但是都是发布在我们自己的个人pc上的. 于是我在想:怎么样让自己的项目可以发到公网上面去,让朋友们能够来访问? 我首先想到的是: ...