Description
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 than n. Can you help me to find the maximum possible least common multiple of these three integers?
Input
The first line contains an integer n (1 ≤ n ≤ 106) — the n mentioned in the statement.
Output
Print a single integer — the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n.
Examples
Input
9
Output
504
Input
7
Output
210
Note
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的三个数的最大公倍数。
分析:首先要知道两个数论性质:1、相邻两个整数互质。2、相邻两个奇数互质。
然后,小数据观察规律,按
n
n
n 的奇偶性讨论。
一、
n
n
n 为奇数时,
n
−
2
n-2
n−2 必定为奇数,而
n
−
1
n-1
n−1 与它俩都相邻,因此,两两互质。最大公倍数即为
n
∗
(
n
−
1
)
∗
(
n
−
2
)
n*(n-1)*(n-2)
n∗(n−1)∗(n−2)。
二、
n
n
n 为偶数时,由于
n
n
n 与
n
−
2
n-2
n−2 均为偶数,所以,退而求其次,选择
n
−
3
n-3
n−3 。又
n
n
n 与
n
−
3
n-3
n−3 是否互质,衍生出两种情况:
1、
n
n
n 为
3
3
3 的倍数,则
n
n
n 与
n
−
3
n-3
n−3 不互质,然而,
n
n
n 与
n
−
4
n-4
n−4 不互质,所以,此时,不选择
n
n
n,改为选
n
−
2
n-2
n−2。最后,
L
C
M
=
(
n
−
1
)
∗
(
n
−
2
)
∗
(
n
−
3
)
LCM = (n-1)*(n-2)*(n-3)
LCM=(n−1)∗(n−2)∗(n−3)
2、
n
n
n不为
3
3
3 的倍数,则
n
n
n 与
n
−
3
n-3
n−3 互质,所以,
L
C
M
=
n
∗
(
n
−
1
)
∗
(
n
−
3
)
LCM = n*(n-1)*(n-3)
LCM=n∗(n−1)∗(n−3)
#include<cstdio>
typedef long long ll;
int main()
{
ll n;
scanf("%lld",&n);
if(n==1)puts("1");
else if(n==2)puts("2");
else if(n&1)printf("%lld\n",n*(n-1)*(n-2));
else if(n%3==0)printf("%lld\n",(n-1)*(n-2)*(n-3));
else printf("%lld\n",n*(n-1)*(n-3));
return 0;
}
总结
数学是一生之敌。巧妙利用互质的性质。