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 ...
随机推荐
- 经典分类CNN模型系列其五:Inception v2与Inception v3
经典分类CNN模型系列其五:Inception v2与Inception v3 介绍 Inception v2与Inception v3被作者放在了一篇paper里面,因此我们也作为一篇blog来对其 ...
- axios接口封装
axios封装 import JsonP from 'jsonp' import axios from 'axios' import { Modal } from 'antd' export defa ...
- 在mac下怎么配置web环境(php)
1, 安装PHP+apach+mysql(xampp) 2, 在目录下建一个新文件夹 : 我是在Users/个人目录/workspace 3, 打开/Applications/XAMPP/xamp ...
- [编织消息框架][netty源码分析]5 EventLoopGroup 实现类NioEventLoopGroup职责与实现
分析NioEventLoopGroup最主有两个疑问 1.next work如何分配NioEventLoop 2.boss group 与child group 是如何协作运行的 从EventLoop ...
- git commit规范工具
npm install -g commitizen commitizen init cz-conventional-changelog --save --save-exact 以后,凡是用到git c ...
- Tool-XManager:XManager(远端X窗口系统的工具)
ylbtech-Tool-XManager:XManager(远端X窗口系统的工具) Xmanager是一款小巧.便捷的浏览远端X窗口系统的工具.在工作中经常使用Xmanager来登录远端的Solar ...
- IO流18 --- RandomAccessFile实现数据的读写操作 --- 技术搬运工(尚硅谷)
RandomAccessFile实例化时,需要设置读写模式 示例:复制文件 @Test public void test16() throws IOException { RandomAccessFi ...
- AT2164 Rabbit Exercise
传送门 解题思路 首先考虑k=1的情况,对于每一个a[i],它可能会到a[i-1]*2-a[i] 与 a[i+1]*2-a[i]两个位置,概率都为%50,那么它的期望位置为 (a[i-1]*2-a[i ...
- PHP--http_build_query--把数组化成&key=value方式
- web前端学习(四)JavaScript学习笔记部分(7)-- JavaScript DOM对象控制HTML元素详解
1.方法 getElementsByName() 获取name 可以获取一个数组类型数据(参数加引号) getElementsByTagName() 获取元素 getAttribute() 获取元 ...