LCM Challenge
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
Output
Sample Input
9
Sample Output
504
Source
Manager
题意:求不超过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。最后最大公倍数即为(n-1)*(n-2)*(n-3)
2、n不为3的倍数,则n与n-3互质,最后最大公倍数即为 n*(n-1)*(n-3)
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define ll long long
int main()
{
ll n;
while(~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;
}