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
 
 
如下为一般方法写的代码
#include<iostream>
using namespace std;
int s[];
__int64 x;
int cmp ( const void *a , const void *b )
{
return *(int *)b - *(int *)a;
}
void f(int s[],int n)
{
int p=;
bool flag;
while(){
flag=true;
x=s[]*p;
for(int i=;i<n;i++){
if(x%s[i]!=){
flag=false;
break;
}
}
if(flag==true)break;
p++;
}
cout<<x<<endl;
}
int main()
{
int n;
cin>>n;
while(n--){
int a;
cin>>a;
for(int i=;i<a;i++)cin>>s[i];
qsort(s,a,sizeof(s[]),cmp);
f(s,a);
}
return ;
}

如下为运用辗转相除法写的代码

#include<iostream>
using namespace std;
int s[];
__int64 x,y;
int g(int a,int b)
{
int i,t;
y=a*b;
if(a<b){
t=a;
a=b;
b=t;
}
while(b){
t=b;
b=a%b;
a=t;
}
return y/a;
}
void f(int s[],int n)
{
x=s[];
for(int i=;i<n;i++){
x=g(s[i],x);
}
cout<<x<<endl;
}
int main()
{
int n;
cin>>n;
while(n--){
int a;
cin>>a;
for(int i=;i<a;i++)cin>>s[i];
f(s,a);
}
//system("pause");
return ;
}

L - 辗转相除法(第二季水)的更多相关文章

  1. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  2. D - Counterfeit Dollar(第二季水)

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  3. N - Robot Motion(第二季水)

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  4. S - 骨牌铺方格(第二季水)

    Description          在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种, ...

  5. R - 一只小蜜蜂...(第二季水)

    Description          有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数.         其中,蜂房的结构如下所示.     ...

  6. I - Long Distance Racing(第二季水)

    Description Bessie is training for her next race by running on a path that includes hills so that sh ...

  7. Y - Design T-Shirt(第二季水)

    Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA ...

  8. B - Maya Calendar(第二季水)

    Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old ...

  9. T - 阿牛的EOF牛肉串(第二季水)

    Description          今年的ACM暑期集训队一共有18人,分为6支队伍.其中有一个叫做EOF的队伍,由04级的阿牛.XC以及05级的COY组成.在共同的集训生活中,大家建立了深厚的 ...

随机推荐

  1. UVALive 4123 Glenbow Museum (组合数学)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 易得,当n为奇数或者n<3时,答案为0,否则该序列中必定有(n+4)/2个R ...

  2. 显示查询记录的前n条 mysql limit用法

    按时间倒序出所有的数据 mysql order by createtime desc ; +----+------------------+ | id | id_no | +----+-------- ...

  3. phpMyAdmin批量修改Mysql数据表前缀的方法

    多个网站共用一个Mysql数据库时,为使数据库管理不混乱,一般采用不同的网站使用不同前缀名的方式进行区分.而如何批量修改已有数据库的前缀名 呢?全部导出修改后再导入?还是一个表一个表的修改?今天我要介 ...

  4. webService设置超时时间

    在客户端配置文件中设置: <bindings>      <basicHttpBinding>        <binding name="UrlCrawler ...

  5. js的eval方法

    eval方法: 检查JScript代码,并且执行. 语法: eval(codeString); 参数: codeString是必选项,参数是包含有效的JScript代码的字符串值,这个字符串值将由JS ...

  6. hdu 1428 漫步校园

    http://acm.hdu.edu.cn/showproblem.php?pid=1428 dijstra+dp; #include <cstdio> #include <queu ...

  7. Qt中实现启动画面(延时过程中要加上app.processEvents())

    纵所周之,当一个程序的启动比较耗时的时候,为了不让用户枯燥的等待或者是误以为程序运行异常了,所以我们都会在启动比较耗时的程序中加上启动界面 ,例如office软件等等. 在Qt中实现启动界面,主要就是 ...

  8. CentOS安装错误:no default or ui configuration

    靠,以后再也不用浏览器自带的下载工具下载镜像文件了,原来是下载的不完整,但是显示完全下载完毕了,真特么误导人.文件的checksum不对. references: https://www.centos ...

  9. bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典

    Description 没有几个人知道,奶牛有她们自己的字典,里面的有W (1 ≤ W ≤ 600)个词,每个词的长度不超过25,且由小写字母组成.她们在交流时,由于各种原因,用词总是不那么准确.比如 ...

  10. apache http配置https

    <一,Lamp系统搭建> yum install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-co ...