***************************************转载请注明出处:http://blog.csdn.net/lttree***************************************

Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 28975    Accepted Submission(s): 10905

Problem Description
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
 
Source
 

这道题就是求一堆数的最小公倍数。

要注意,每一行的第一个数表示后面跟着有几个数。

然后,我的方法是将输入的数存到数组,然后进行从大到小排序。

从头開始推断,假设当前的mul(之前全部数的最小公倍数)是当前数的倍数,就不须要计算这个数了,直接跳过。

最后再注意一点,用__int64来解决,scanf是用:%I64d

/****************************************
*****************************************
* Author:Tree *
*From : blog.csdn.net/lttree *
* Title : Least Commn Multiple *
*Source: hdu 1019 *
* Hint : *
*****************************************
****************************************/ #include <stdio.h>
#include <algorithm>
using namespace std; __int64 num[10001]; // 求a和b的最小公倍数
__int64 lcm( __int64 a,__int64 b)
{
__int64 x=a,y=b,k;
while( x%y!=0 )
{
k=x%y;
x=y;
y=k;
}
return a*b/k;
}
// 从大到小排序 sort用
bool cmp(__int64 a,__int64 b)
{
return a>b;
}
int main()
{
int t,n,i;
__int64 mul; scanf("%d",&t);
while( t-- )
{
scanf("%d",&n);
for( i=0;i<n;++i )
scanf("%I64d",&num[i]);
sort(num,num+n,cmp);
mul=num[0];
// 假设当前的最小公倍数是该数的倍数就不须要计算了。
for( i=1;i<n;++i )
if( mul%num[i]!=0 )
mul=lcm(mul,num[i]);
printf("%I64d\n",mul);
}
return 0;
}

ACM-简单题之Least Common Multiple——hdu1019的更多相关文章

  1. 2道acm简单题(2010):1.猜数字游戏;2.字符串提取数字并求和;

    //第一题是猜数字的游戏.//题目:随即产生一个3位的正整数,让你进行猜数字,//如果猜小了,输出:"猜小了,请继续".//如果猜大了,输出:"猜大了,请继续" ...

  2. 2道acm简单题(2013):1.(时分秒)时间相减;2.主持人和N-1个人玩游戏,每个人说出自己认识的人数,判断其中是否有人说谎。

    /*1.题目:输入一个数,代表要检测的例子的个数,每个例子中:输入两个时间(格式HH:MM : SS),前面时间减去后面时间,输出在时钟上显示的时间,格式一样,如果是以为数字的前面补零.*//**思路 ...

  3. 3道acm简单题(2011):1.判断是否能组成三角形;2.判断打鱼还是晒网;3.判断丑数。

    //1.输入三个正整数A.B.C,判断这三个数能不能构成一个三角形.//思路:最小的两边之和是否是大于第三边#include<iostream>#include<algorithm& ...

  4. HDU1019 Least Common Multiple(多个数的最小公倍数)

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  5. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

  6. HDU-1019 Least Common Multiple

    http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/ ...

  7. ACM hdu 1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  8. acm.njupt 1001-1026 简单题

    点击可展开上面目录 Acm.njupt 1001-1026简单题 第一页许多是简单题,每题拿出来说说,没有必要,也说不了什么. 直接贴上AC的代码.初学者一题题做,看看别人的AC代码,寻找自己的问题. ...

  9. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

随机推荐

  1. css盒模型和块级、行内元素深入理解

    盒模型是CSS的核心知识点之一,它指定元素如何显示以及如何相互交互.页面上的每个元素都被看成一个矩形框,这个框由元素的内容.内边距.边框和外边距组成,需要了解的朋友可以深入参考下 一.CSS盒模型 盒 ...

  2. 关于ARM开发板与PC主机的网络设置问题

    直观来讲,ARM开发板多数情况下会有条网线与主机相连,所以最重要的一步是保证PC主机与ARM开发板能互通. 互通的意思进一步来讲就是互相能ping通.也就是说在瘟都死的dos下(假设主机是瘟都死系统) ...

  3. 从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介

    一.C语言错误处理方法 1.返回值(if … else语句判断错误) 2.errno(linux 系统调用) 3.goto语句(函数内局部跳转) 4.setjmp.longjmp(Do not use ...

  4. Android自学绝佳资料

    本文转自stormzhang老师的博客:http://stormzhang.com/android/2014/07/07/learn-android-from-rookie 首先感谢stromzhan ...

  5. nginx的sendfile指令的作用

    linux为了解决对读文件产生的从应用空间到内核空间复制数据产生的效率影响引进了零拷贝.什么是零拷贝?这里就不多说了,请参考http://blog.csdn.net/crazyguang/articl ...

  6. C2B未来:大数据定制

    昨天看到微信SuperSofter写了一篇文章,有感而发.以便备记. 这是一种典型的C2B模式.阿里不仅仅是在与腾讯拼移动.它的电商本土业务也在稳步推进.近期一个里程碑事件是.阿里包下了美的.九阳.苏 ...

  7. ALV预警灯图标代码

    需要先引用TYPE-POOLS: slis,icon. ICON_LED_GREEN 绿灯 ICON_LED_RED红灯 ICON_LED_YELLOW黄灯

  8. 深入探讨MFC消息循环和消息泵

    首先,应该清楚MFC的消息循环(::GetMessage,::PeekMessage),消息泵(CWinThread::PumpMessage)和MFC的消息在窗口之间的路由是两件不同的事情.在MFC ...

  9. 基于MMSeg算法的中文分词类库

    原文:基于MMSeg算法的中文分词类库 最近在实现基于lucene.net的搜索方案,涉及中文分词,找了很多,最终选择了MMSeg4j,但MMSeg4j只有Java版,在博客园上找到了*王员外*(ht ...

  10. Nginx和Tomcat负载均衡实现session共享(转)

    以前的项目使用Nginx作为反向代理实现了多个Tomcat的负载均衡,为了实现多个Tomcat之间的session共享,使用了开源的Memcached-Session-Manager框架. 此框架的优 ...