The largest least common multiple (analysis questions, must have common sense of mathematics) HRBUST-1632

Question link

We have learned the least common multiple since elementary school, and today's question is also about the least common multiple lcm (lease common multiple). Our problem is that, given an integer n, you need to choose any three numbers not greater than n. There are no restrictions on how to choose. Each number can be more than one, so that the least common multiple of these three numbers is in all the methods. Is the biggest.

For example: the given n is 5. Then the optional numbers not greater than 5 are 1, 2, 3, 4, 5. The least common multiple of the three numbers 3, 4, and 5 selected here is 60, which is the largest among all methods. So we get the result 60.

Input

The input contains multiple sets of test data, each of which is an integer n (1 <= n <= 10^6) as mentioned above.

Output

For each set of test data, output an integer, which represents the maximum value of the least common multiple of three numbers that do not exceed n among all possible options. It is allowed to select the same number multiple times.

Sample Input
5

7 
Sample Output
60

210 
Before writing this question, you must know a few mathematical knowledge:

1. Any two adjacent numbers greater than 1 are mutually prime
2. Any two adjacent two odd numbers greater than 1 are mutually prime
3. If three numbers are relatively prime, its least common multiple is their product

For the input number n, there are three situations:
1. n is an odd number. At this time, the three adjacent numbers not greater than n are odd-even-odd. According to the above three mathematical knowledge, we can know these three numbers n, n-1 , N-2 is relatively prime.
2. n is an even number. At this time, the three adjacent numbers not larger than n are even-odd-even. These two even numbers must not be mutually prime. In order to maximize the product, we move the smallest even number backward by 1, that is, at this time The three numbers are n, n-1, n-3, at this time n and n-1 are relatively prime, n-1 and n-3 are relatively prime, but n and n-3 are not necessarily relatively prime, because when n is When it is a multiple of 3, n and n-3 must have a common factor of 3. At this time, we will discuss classification, so when n is not a multiple of 3, n, n-1, and n-3 must be relatively prime.
3. When n is an even number and n is a multiple of 3, we try to move the smallest number backward by 2 (or an even number if you move 1), and get three numbers as n, n-1, n-5, if These three numbers are OK, then the score n×(n-1)×(n-5) must be less than (n-1)×(n-2)×(n-3), why and (n-1)×( n-2)×(n-3) compare, because according to the first article (n-1), (n-2), (n-3) must be relatively prime. Therefore, in this case, use (n-1), (n-2), (n-3) directly

Finally, pay attention to some small details: when the input n is less than 3, the result is n itself.

code show as below:

#include<iostream>
using namespace std;
int main()
{
    
    
	long long n=0;
	while(cin>>n)
	{
    
    
		if(n<3)
		{
    
    
			cout<<n<<endl;
			continue;
		}
		if(n%2==1)
		{
    
    
			cout<<n*(n-1)*(n-2)<<endl;
		}
		else
		{
    
    
			if(n%3!=0)
			cout<<n*(n-1)*(n-3)<<endl;
			else
			cout<<(n-1)*(n-2)*(n-3)<<endl;
		}
	} 
	return 0;
}

Guess you like

Origin blog.csdn.net/Huo6666/article/details/108816224