hdu acmsteps 2.1.1 最小公倍数(GCD)
2013-04-06 00:17
417 查看
最小公倍数
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2010 Accepted Submission(s): 1543
Problem Description
给定两个正整数,计算这两个数的最小公倍数。
Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.
Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。
Sample Input
10 14
Sample Output
70
Source
POJ
Recommend
Eddy
#include<iostream>
[code]using namespace std;
int gcd(int a,int b){
return (b==0 ? a: gcd(b,a%b));
}
int main(){
for(int n,m;cin>>n>>m;){
cout<<n/gcd(n,m)*m<<'\n';
}
}
[/code]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
辗转相除法(又称为“欧几里得算法”):
恒等式:gcd(a,b)=gcd(b,a%b);
边界条件:gcd(a,0)=a;
gcd函数的递归层数不超过4.785lg(N)+1.6723, 其中N=max{ a,b };
值得一提的是,让gcd递归层数最多的是gcd(F(n),F(n-1)),F(n)为fibonacci数列;
(摘自刘汝佳白书,P117);
相关文章推荐
- hdu acm steps 2.1.1
- ACM--steps--dyx--2.1.1--最小公倍数
- hdu ACM STEPS 1.2.7
- HDU ACMSteps 1.3.3:: Tian Ji ——The Horse Racing
- hdu acmsteps 2.2.1 Fibonacci
- HDU 1108 最小公倍数(GCD,LCM)
- hdu ACM steps 1.2.7 hdu 2629
- HDU ACMSteps 1.3.2:: 今年暑假不AC(动态规划)
- HDU ACM Steps攻略 ACM Steps的全部内容
- HDU 2.1.1最小公倍数
- hdu2.1.1 最小公倍数
- hdu-acm stepsHumble Numbers
- hdu acmsteps 2.3
- (hdu step 2.1.1)最小公倍数(使用最大公约数来辅助求最小公倍数)
- hdu 1108 最小公倍数 (GCD)
- http://acm.hdu.edu.cn/showproblem.php?pid=1108 java BIgInteger gcd
- hdu 5869 Different GCD Subarray Query 2016ACM/ICPC大连赛区网络赛1006
- [小结]HDU ACM Steps
- acm--最小公倍数(HDU 1108)
- hdu 4497 GCD and LCM(2013 ACM-ICPC吉林通化全国邀请赛——题目重现)