Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it.
But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater thann. Can you help me to find the maximum possible least common multiple of these three integers?
The first line contains an integer n (1 ≤ n ≤ 106) — the n mentioned in the statement.
Print a single integer — the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n.
9
504
7
210
The least common multiple of some positive integers is the least positive integer which is multiple for each of them.
The result may become very large, 32-bit integer won't be enough. So using 64-bit integers is recommended.
For the last example, we can chose numbers 7, 6, 5 and the LCM of them is 7·6·5 = 210. It is the maximum value we can get.
水题。
判断n是奇数还是偶数,如果n是奇数,(n-2)也是奇数,满足条件的情况就是 n*(n-1)*(n-2)
若n是偶数,则比它小的奇数是(n-1),一种情况是(n-1)*(n-2)*(n-3),另一种就是n*(n-1)*(n-3)/gcd(n,n-3) ,求这两者的最大值 整的来说可以使一个模3体系
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
__int64 gcd(__int64 a,__int64 b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int main()
{
__int64 n,ans=1;
scanf("%I64d",&n);
if(n==1)
{
printf("1\n");
return 0;
}
else if(n==2)
{
printf("2\n");
return 0;
}
if(n%2==1)
{
ans=n*(n-1)*(n-2);
printf("%I64d\n",ans);
return 0;
}
else
{
ans=n*(n-1)*(n-3);
ans/=gcd(n,n-3);
ans=max(ans,(n-3)*(n-1)*(n-2));
printf("%I64d\n",ans);
}
return 0;
}