Blue Bridge cup: Maximal least common multiple of algorithm training

Source: Internet
Author: User
Tags greatest common divisor

Algorithm Training Max least common multiple
Time limit: 1.0s Memory limit: 256.0MB
Problem description

A positive integer n is known, asking for three numbers from 1~n, and the maximum number of least common multiple they can Have.
Input format

Enter a positive integer N.
Output format
Output An integer that represents the least common multiple you found.
Sample input
9
Sample output
504
Data size and conventions

1 <= N <= 106.

note: greedy, starting from the largest number of three to consider, if the largest number is odd, then the adjacent three numbers have two odd, greatest common divisor 1, least common multiple is n (n-1) (n-2). If an even number, then move backward, consider N (n-1) (n-3), when N and n-3 difference 3, The premise that the formula satisfies the condition is that n can not be divisible by 3, otherwise the result can only be (n-1) (n-2) (n-3).

The code is as Follows:

package com.sihai.advance;import java.util.scanner;public class tanxin {     public static void main (string[] args)  {         scanner scanner = new scanner (system.in);         long n = scanner.nextlong ();          long result;        if (n<3)                result=n;           else{          if (n%2!=0)                result=n* (n-1) * (n-2);           else if (n%3!=0)                result=n* (n-1) * (n-3);          else               result= (n-1) * (n-2) * (n-3);           }           System.out.println (result);     }} 1234567891011121314151617181920212223242512345678910111213141516171819202122232425

The greedy algorithm can look at the following content

first, the basic concept:

The so-called greedy algorithm refers to, in the problem solving, always make the best choice at Present. In other words, not considering the overall optimality, he makes only the local optimal solution in a certain sense. Greedy algorithm has no fixed algorithm framework, the key of algorithm design is the choice of greedy strategy. It must be noted that the greedy algorithm does not have the overall optimal solution to all problems, the greedy strategy chosen must have no effect, that is, the process after a state does not affect the previous state, only related to the current State. therefore, The greedy strategy used must carefully analyze whether it satisfies the No-effect.

second, The basic idea of greedy algorithm:
1. Build a mathematical model to describe the Problem.
2. Divide the problem of solving into several sub-problems.
3. To solve each sub-problem, the local optimal solution of Sub-problem is Obtained.
4. The solution of the problem is solved by the local optimal solution to the original solution Problem.

three, The greedy algorithm applies the question
The premise of greedy strategy is that local optimal strategy can lead to global optimal Solution.
In fact, greedy algorithms are rarely used. In general, whether a problem analysis is applicable to greedy algorithm, you can choose the problem of a few real data analysis, you can make Judgments.

four, Greedy Algorithm Implementation Framework
Starting from an initial solution of a problem;
While (one step forward for a given total Target)
{
A solution element of the feasible solution is obtained by using the feasible decision;
}
A feasible solution to the problem of the combination of all solution elements;

five, the choice of greedy strategy
Because the greedy algorithm can only solve the global optimal solution by solving the local optimal solution, we must pay attention to whether the problem is suitable for the greedy algorithm and whether the solution found is the best solution.

Vi. Analysis of examples
Here is a greedy algorithm can be tried to solve the problem, greedy solution is really good, but not the optimal Solution.
[knapsack problem] There is a backpack, the backpack capacity is m=150. There are 7 items that can be divided into any size.
It is required to maximize the total value of the items loaded into the backpack, but not to exceed the total Capacity.
Item A B C D E F G
Weight 35 30 60 50 40 10 25
Value 10 40 30 50 35 40 30
Analysis:
Objective function: ∑PI Max
The restriction is that the total weight of the loaded item does not exceed the backpack capacity: ∑wi<=m (m=150)
(1) according to the greedy strategy, each time to select the most valuable items loaded into the backpack, the results are optimal?
(2) can the optimal solution be obtained for each item with the smallest weight selected?
(3) each time the unit weight value of the most valuable items, become the solution of the Strategy.
It is worth noting that the greedy algorithm is not completely not available, once the greedy strategy has been proved, it is an efficient algorithm.
Greedy algorithm is also a very common algorithm, this is because it is simple, the construction of greedy strategy is not very difficult.
unfortunately, it needs to be proven before it can really be used in the algorithm of the Problem.
In general, the proof of greedy algorithm revolves around: the optimal solution of the whole problem must be obtained by the optimal solution of the sub-problem in the greedy strategy.
For the 3 kinds of greedy strategies in the example, it is impossible to set up (can not be proved), explained as Follows:
(1) Greedy Strategy: Choose the most valuable Person. Counter Example:
W=30
Item: A B C
Weight: 28 12 12
Value: 30 20 20
According to the strategy, first select item a, then you can no longer select, however, choose b, C is better.
(2) greedy strategy: Choose the smallest weight. Its inverse example is similar to the first Strategy's counter Example.
(3) greedy strategy: Select items with the greatest value per unit weight. Counter Example:
W=30
Item: A B C
Weight: 28 20 10
Value: 28 20 10
According to the strategy, three items per unit weight value, The program can not be judged according to the existing policy, if you choose a, the answer is Wrong.


This article is from the "11115680" blog, please be sure to keep this source http://11125680.blog.51cto.com/11115680/1909511

Blue Bridge cup: Maximal least common multiple of algorithm training

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.