HDU-1019 least common multiple

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

表面上要求计算最小公倍数,实则是用欧几里得算法求LCD。

注意计算顺序,否则溢出!

/*
 * HDU-1019 least common multiple
 * mike-w
 * 2011-8-30
 * ------------------------------
 * calculate GCD
 */
#include<stdio.h>

long gcd(long e1,long e2)
{
	long r=e1%e2;
	while(r)
	{
		e1=e2;
		e2=r;
		r=e1%e2;
	}
	return e2;
}

int main(void)
{
	int ncase,n,i;
	long e1,e2;

#ifndef ONLINE_JUDGE
	freopen("1019.in","r",stdin);
#endif

	scanf("%d",&ncase);
	for(i=1;i<=ncase;i++)
	{
		scanf("%d",&n);
		scanf("%ld",&e1);
		n--;
		while(n-->0)
		{
			scanf("%ld",&e2);
			e1=e1/gcd(e1,e2)*e2;	/* 这里的运算顺序需要注意一下,否则溢出 */
		}
		printf("%ld\n",e1);
	}
	return 0;
}


 

ACM-简单题之Least Common Multiple——hdu1019 ACM 简单题 Least Common Multiple hdu1019 最小公倍数 阅读详情

相关推荐

hdu 1019 Least Common Multiple

hdu 1019 Least Common Multiple添加链接描述 #include <bits/stdc++.h> using namespace std; int gcd(int x, int y){ return y ? gcd(y, x % y) : x; } int lcm(int x, int y){ return x/gcd(x,y)*y; } int main(){ int T, m, x, ans; scanf("%d", &T);

weixin_51663098的博客 123

HDU-1019 Least Common Multiple

HDU 1019 Least Common Multiple 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 ...

你呢的博客 817

HDU1019 Least Common Multiple

HDU 1019Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 68851 Accepted Submission(s): 26323 Problem Descrip...

码农-Vax天道的时间宝藏 408

ACM hdu 1019 Least Common Multiple

ACM hdu 1019 Least Common Multiple Problem Description The least common multiple (LCM) of a set of positive integers is t...

dilan3700的博客 207

hdu 1019 Least Common Multiple

hdu  1019  Least Common Multiple             题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 数学水 题目大意:求一串数的最小公倍数。 题目分析:每输入一个数,求一下中间结果。 code: #include int gcd(int a,int b) { while(b^=a^=b^=a%

slicer的技术交流栈 675

算法模板之欧几里得算法HDU1019 Least Common Multiple

模板总结归纳://欧几里得算法(辗转相除法) //O(logN) /*int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } */实战模板题 :HDU1019 Least Common Multiple#include&lt;iostream&gt; #include&lt;cstdio&gt; using namespa...

摸不着脑子的博客 244

hdu1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28021Accepted ...

chongkuachan8070的博客 151

HDU ACM1019——Least Common Multiple

Least Common Multiple 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 examp...

BarisGuo的博客 335

HDU 1019 Least Common Multiple

Problem DescriptionThe 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 1

Czy 1542

hdu1019——Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42668    Accepted Submission(s): 16035 Problem Description The le

pjr要努力的博客 366

hdu 1019 least common multiple

least common multiple 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

星空永恒 569

Least Common Multiple HDU 1019

数论 最大公约数 最小公倍数

分享知识,成就你我 252
上一篇: HDU-1017 mathematical curiosity
下一篇: HDU-1016 prime ring problem
creativewang
博客等级 码龄17年 20粉丝 203原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值