# Question

Formatted question description: https://leetcode.ca/all/650.html

 650. 2 Keys Keyboard

Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:

Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
Paste: You can paste the characters which are copied last time.

Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'.

Example 1:

Input: 3
Output: 3
Explanation:
Intitally, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.

Note:
The n will be in the range [1, 1000].



# Algorithm

This question only gave us two buttons. If you can only choose two buttons, then I will definitely have to copy and paste. These two buttons are in hand, I have the world! ! ! Sure enough, this question is to give the two buttons of copy and paste, and then give an A.

These two keys are used to print out n A. Note that when copying, all are copied, and you cannot select parts to copy. Then copy and paste are both operation steps, ask how many steps are needed to print out n A.

For this kind of problem with obvious recurrence characteristics, you must have a vague feeling, and you must try recursion and DP. Recursive solutions are generally close to brute force search, but sometimes they can be optimized to pass OJ. If recursion is not possible, then DP can be solved generally.

Another point is that for this kind of problem, finding the law is the most important. DP must find the state transition equation, and if the inner connection cannot be found, then the state transition equation is more difficult to write. So, start with a simple example and try to find a pattern:

When n = 1, there is already an A, no other operations are needed, return 0

When n = 2, need to copy once, paste once, return 2

When n = 3, you need to copy once and paste twice, return 3

When n = 4, there are two ways to do this,

• one is to copy once and paste three times, a total of 4 steps,
• the other is to copy once, paste once to get AA, then copy again, paste once to get AAAA, both methods return 4

When n = 5, you need to copy once, paste four times, and return to 5.

When n = 6, you need to copy once and paste twice to get AAA, and copy again and paste once to get AAAAAA, 5 steps in total, return to 5.

By analyzing the above 6 simple examples, we can conclude some rules.

1. First of all, for any n (except 1), the worst case is to use n steps, no more than n steps,
2. but it may be less than n steps. For example, when n=6, only 5 steps are used. After careful analysis, it is first spelled as AAA, and then copied and pasted to AAAAAA.
3. So under what circumstances can this method be used to reduce steps? Analysis found that the length of the small module must be divisible by n in order to be split. For n=6, we can actually print AA first, then copy and paste it twice, and get 5 again.

At this point in the analysis, the idea of solving the problem should be clearer, find out all the factors of n, and then this factor can be used as the number of modules, and then calculate the length of the module n/i, call recursion, and add the number of modules i To update the result res.

Java