Least Common Multiple (最小公倍数,先除再乘)

#include <iostream>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string.h>
#include <string>
#include <algorithm> using namespace std; int gcd(int a, int b)
{
return b == ? a : gcd(b, a%b);
} int main()
{
int n;
while(cin >> n)
{
while(n--)
{
int m, a, ans;
cin >> m;
cin >> a;
ans = a; // 当前的最小公倍数
while(--m)
{
cin >> a;
ans = ans * (a / gcd(ans, a)); // 这里如果先乘后除的话,可能会出现超出int限制的数。导致提交后WA
}
cout << ans << endl;
}
} return ;
}
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> ...
 - zoj1797 Least Common Multiple 最小公倍数
		
Least Common Multiple Time Limit: 2 Seconds Memory Limit: 65536 KB The least common multiple (L ...
 - 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 ...
 
随机推荐
- Synchronized理解及用法
			
加锁: 1.同步实例方法,锁是当前实例对象 2.同步类方法,锁的是当前类对象 3.同步代码块,锁是括号里面的对象 原理: JVM内置锁通过synchronized使用,通过内部对象Monitor(监视 ...
 - java软引用、弱引用(转摘)
			
本文转自网络,源地址:https://www.jianshu.com/p/b56731447179 一.引用对象类型定义 首先,引用对象在Java定义中有三种类型,从弱到强依次为:软引用.弱引用与虚引 ...
 - [vagrant]vagrant centos静态ip设置
			
vagrant 中使用的是public_network,而工作网络中,由于桥接了很多路由器,导致ip段位和本机的ip段位不在同一个局域网中 ifconfig之后的结果 [root@localhost ...
 - vue.js_05_vue.js的过滤器
			
1.过滤器的定义和使用 实现:将页面的中的单纯替换成,用户传来的文字. 全局过滤器:所有的Vue对象都可以使用 <body> <div id="app"> ...
 - layer时间插件
			
引入: <link rel="stylesheet" href="<{$cdnsite}>/default/common/layui/css/layui ...
 - 转:Linux 2.4.x内核软中断机制
			
源地址:http://www.ibm.com/developerworks/cn/linux/kernel/interrupt/ Linux 2.4.x内核软中断机制 杨沙洲 (pubb@163.ne ...
 - PAT甲级——A1055 The World's Richest
			
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
 - Python - 基本数据类型及其常用的方法之列表
			
列表: 特点:用 [] 括起来,切元素用逗号分隔:列表内的元素可以为任何的数据类型. 列表的基本操作: 1.修改 li = [12, 5, 6, ["Aiden", [2, 4], ...
 - Codeforces 142D(博弈)
			
要点 不难发现问题转化成:n堆石子,每次最多选k堆最少选1堆然后拿走一个石子,谁先没子可拿谁败.本题中撤退不必考虑. 就是记笔记吧,类似nim的博弈,举例:\[k=3,n=4\]\[4堆石子分别是1. ...
 - TZ_09_自定义Spring-security
			
1.Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架 2.安全包括两个主要操作. “认证”,是为用户建立一个他所声明的主体 ...