最小公倍数和最大公约数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
long long com_mul(long long n1,long long n2)
{
long long ans=n1*n2;
long long i=2,limit=sqrt((double)min(n1,n2))+1;
while(i<=n1&&i<=n2&&i<=limit)
{
if(n1%i==0&&n2%i==0)
{
ans/=i;
n1/=i;
n2/=i;
}
else
i++;
}
return ans;
}

long long com_factor(long long n1,long long n2)
{
long long ans=1;
long long i=2,limit=sqrt((double)min(n1,n2))+1;
while(i<=n1&&i<=n2&&i<=limit)
{
if(n1%i==0&&n2%i==0)
{
ans*=i;
n1/=i;
n2/=i;
}
else
i++;
}
return ans;
}