您的位置:首页 > 其它

LCM Challenge(暴力)

2015-05-24 11:43 826 查看
A - LCM Challenge

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Submit Status

Problem 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 ≤ 10^6) — 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.

Sample Input

9

Sample Output

504

题目大意:求不超过n的三个数的最大公倍数。

分析:首先要知道两个数论性质:1、相邻两个整数互质。2、相邻两个奇数互质。

然后,小数据观察规律,按n的奇偶性讨论。

一、n为奇数时,n-2必定为奇数,而n-1与它俩都相邻,因此,两两互质。最大公倍数即为n*(n-1)*(n-2)。

二、n为偶数时,由于n与n-2均为偶数,所以,退而求其次,选择n-3。又n与n-3是否互质,衍生出两种情况:

1、n为3的倍数,则n与n-3不互质,然而,n与n-4不互质,所以,此时,不选择n,改为选n-2。最后,LCM = (n-1)*(n-2)*(n-3)

2、n不为3的倍数,则n与n-3互质,所以,LCM = n*(n-1)*(n-3)

代码:

#include <cstdio>
using namespace std;

int main() {
long long n;
scanf("%lld", &n);
if(n == 1) printf("1\n");
else if(n == 2) printf("2\n");
else if(n%2 != 0) 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数论 暴力 ACdream