【题解】codeforces235A[AHSOFNU codeforces训练赛2 by hzwer]B.LCM Challenge 数学知识

32 篇文章 0 订阅
32 篇文章 0 订阅

题目链接

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 n2 必定为奇数,而 n − 1 n-1 n1 与它俩都相邻,因此,两两互质。最大公倍数即为 n ∗ ( n − 1 ) ∗ ( n − 2 ) n*(n-1)*(n-2) n(n1)(n2)
二、 n n n 为偶数时,由于 n n n n − 2 n-2 n2 均为偶数,所以,退而求其次,选择 n − 3 n-3 n3 。又 n n n n − 3 n-3 n3 是否互质,衍生出两种情况:
1、 n n n 3 3 3 的倍数,则 n n n n − 3 n-3 n3 不互质,然而, n n n n − 4 n-4 n4 不互质,所以,此时,不选择 n n n,改为选 n − 2 n-2 n2。最后, L C M = ( n − 1 ) ∗ ( n − 2 ) ∗ ( n − 3 ) LCM = (n-1)*(n-2)*(n-3) LCM=(n1)(n2)(n3)
2、 n n n不为 3 3 3 的倍数,则 n n n n − 3 n-3 n3 互质,所以, L C M = n ∗ ( n − 1 ) ∗ ( n − 3 ) LCM = n*(n-1)*(n-3) LCM=n(n1)(n3)

#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;
}

总结

数学是一生之敌。巧妙利用互质的性质。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值