基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
输入2个正整数A,B,求A与B的最小公倍数。
Input
2个数A,B,中间用空格隔开。(1<= A,B <= 10^9)
Output
输出A与B的最小公倍数。
Input示例
30 105
Output示例
210
#include<iostream>
using namespace std;
typedef long long ll;
ll gcd(ll m,ll n)
{
return (m==0)?n:gcd(n%m,m);
}
ll lcm(ll a,ll b)
{
return (a/gcd(a,b)*b);
}
int main()
{
int a,b;
cin>>a>>b;
cout<<lcm(a,b)<<endl;
return 0;
}
扩展欧几里得
/*
* 求x,y使得gcd(a, b) = a * x + b * y;
*/
int extgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int d = extgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return d;
}