LCM Challenge
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
21
Tips
题意:
给定一个正整数
n
n
n ,求用不大于
n
n
n 的三个正整数(可以有相同)能组成的最小公倍数的最大值。
题解:
法一:
最终的答案一定在
n
n
n 的附近,因此可以确定一个小范围然后暴力一波。
法二:
首先考虑
n
n
n 的奇偶性。
若
n
n
n 是奇数,则
n
,
n
−
1
,
n
−
2
n,n-1,n-2
n,n−1,n−2 两两互质,因此这三个数的积即为所求。
若
n
n
n 是偶数,则
n
,
n
−
1
,
n
−
2
n,n-1,n-2
n,n−1,n−2 三个数就不能选了,因为
n
n
n 和
n
−
2
n-2
n−2 有公因子
2
2
2 ,不划算。考虑换一个积比这个组合小但彼此互质的组合。首先想到
n
,
n
−
1
,
n
−
3
n,n-1,n-3
n,n−1,n−3 。这里还要再考虑
n
n
n 是否是
3
3
3 的倍数。 若
n
n
n 不是
3
3
3 的倍数,很好,彼此互质,即为所求。若是,则还要再考虑一个积更小的组合,想到
n
−
1
,
n
−
2
,
n
−
3
n-1,n-2,n-3
n−1,n−2,n−3 。
综上,当 n n n 是奇数时,答案为 n ( n − 1 ) ( n − 2 ) n(n-1)(n-2) n(n−1)(n−2) ;当 n n n 是 6 6 6 的倍数时,答案为 ( n − 1 ) ( n − 2 ) ( n − 3 ) (n-1)(n-2)(n-3) (n−1)(n−2)(n−3) ;否则,答案为 n ( n − 1 ) ( n − 3 ) n(n-1)(n-3) n(n−1)(n−3) 。(当 n n n 较小的时候还需要特判一下)
Reference Code
#include <cstdio>
const int MAXN=1e6+10;
typedef long long ll;
ll n,res;
int main(){
// freopen("in.txt","r",stdin);
scanf("%lld",&n);
if (n<=2) res=n;
else if (n&1) res=n*(n-1)*(n-2);
else{
if (n%3==0) res=(n-1)*(n-2)*(n-3);
else res=n*(n-1)*(n-3);
}
printf("%lld\n",res);
return 0;
}
//#include <cstdio>
//#include <algorithm>
//typedef long long ll;
//inline ll lcm(ll a,ll b){
// return a*b/std::__gcd(a,b);
//}
//int n;
//ll solve(){
// ll res=0;
// int lowwer=std::max(1,n-100);
// for (int i=lowwer;i<=n;++i){
// for (int j=lowwer;j<=n;++j){
// for (int k=lowwer;k<=n;++k){
// res=std::max(res,lcm(lcm(i,j),k));
// }
// }
// }
// return res;
//}
//int main(){
freopen("in.txt","r",stdin);
// scanf("%d",&n);
// printf("%lld\n",solve());
// return 0;
//}