It is trivial to notice that for <= 6, N. But how about the case where = 7? Here is where the most common pitfall that most people would fall into (myself included).

Most people reason that for = 7, the answer is = 8, because the sequence S = { A, A, A, A, CTRL+A, CTRL+C, CTRL+V } produces a total of 8 A’s.

Wait, the copied text is still in the buffer after a paste operation. We could have applied CTRL+V twice to double the previous text, sweet!   

How about S = { A, A, A, CTRL+A, CTRL+C, CTRL+V, CTRL+V } which produces a total of 9 A’s?

Unfortunately, both of the above answers are incorrect, as the correct answer for N = 7 is M = 7. This is simply because the sequence of { CTRL+A, CTRL+C, CTRL+V } does not double the previous text. Why? Take a moment to let this to sink into your brain.

Answers for N up to 7 is easy, which is M = N. But how about N = 8?

For N = 8 the answer is M = 9, where S = { A, A, A, CTRL+A, CTRL+C, CTRL+V, CTRL+V, CTRL+V }.

For N = 9 the answer is M = 12, where S = { A, A, A, CTRL+A, CTRL+C, CTRL+V, CTRL+V, CTRL+V, CTRL+V }.

You might ask why all A’s are typed before the sequence of { CTRL+A, CTRL+C, CTRL+V } operations.

Assume that we could insert A’s at the back of some sequence of { CTRL+A, CTRL+C, CTRL+V } and yield a maximum sequence. If we take all the A’s from the back and insert it at the front, this modified sequence must yield a larger number of A’s, since the number of A’s is multipled from the beginning. Therefore, by contradiction, all A’s must always be inserted at the front to yield the maximum number of A’s. Similar for the case where A’s are inserted in the middle of the sequence.

Before we proceed further, we introduce the following notation:

  • Define 4A as a sequence of { A, A, A, A }. Therefore, 5A would then mean { A, A, A, A, A }.
  • Define 2D as a sequence of CTRL+A, CTRL+C, CTRL+V, CTRL+V, which simply means double the previous text. Note that 3D does not double the previous text, it actually triples the previous text.

With this notation in place, it is much easier to work with this problem. Using the above notation, we rewrite our answer for = 8 and = 9.

= 8, S = { 3A3D },   = 9.

= 9, S = { 3A4D },   = 12.

The value of M could be obtained simply by multiplying the numbers, isn’t that neat?

Working our way up:
= 10, S = { 4A4D },   = 16.

N = 11, S = { 5A4D },   = 20.

As you can see, the pattern here is pretty obvious, let’s summarize as follow:

  • The solution so far for > 7 is to find integers a and b such that ab yields the largest product, subjected to the condition where a+b = N-2.
  • Both and are easy to find, as the largest product is found when the difference of and is less than or equal to one.

Similarly,
= 12, S = { 5A5D },   = 25.
= 13, S = { 5A6D },   = 30.
= 14, S = { 6A6D },   = 36.

Be extra cautious for = 15.
When = 15, does the sequence { 6A7D } yields the maximum where = 42?

Imagine if you have a very large number of keystrokes to enter, does pressing CTRL+V forever gives you the maximum sequence? Remember, you can redo the entire { CTRL+A, CTRL+C, CTRL+V } operations again and potentially maximizes the sequence.

For = 15, the maximum sequence should be:
3A4D4D }, which yields = 48.

Similarly,
N = 16, = { 4A4D4D },   = 64.

N = 21, = { 3A4D4D4D },   = 192.

= 25, = { 4A5D5D5D },   = 500.
= 26, = { 5A5D5D5D },   = 625.
= 27, = { 3A4D4D4D4D },   = 768.

Let’s generalize the above:

M = MAX ( a 1 .  a 2 …  ak),

where 
a
1 + a2 + … + ak = n – 2(k-1)

To obtain M = MAX (a1 . a2 … ak),
it is necessary that the below condition must be met:

∀  ij ∈{ 1, 2, … ,  } :   MAX ( |  ai –  aj | ) = 1.

To obtain M, we can first divide a1 + a2 + … + ak by k to obtain the average as a reference, and the rest should be straightforward.

Now the final problem lies in how to obtain the value of k efficiently. I am pretty sure this could be solved easily using Number Theory, but so far, my best solution is to use a brute force method to obtain k.

Below is the C++ code for my solution. It is pretty straightforward to output the sequence S. Given N, the functionf() returns the maximum value of M.

#include <iostream>
#include <cmath>
#include <cassert>
usingnamespace std;
 
intfindMaxK(intn) {
    intpower = 2;
    doublemax = 0.0;
    intmaxK = 0;
    while(n > 0) {
        n -= 2;
        doublet = (double)n/power;
        doubler = pow(t, (double)power);
        if(r > max) {
            maxK = power;
            max = r;
        }
        power++;
    }
    returnmaxK;
}
 
unsignedintf(intn) {
    if(n <= 7) returnn;
    intk = findMaxK(n);
 
    intsum = n - 2*(k-1);
    unsignedintmul = 1;
    while(k > 0) {
        intavg = sum/k;
        mul *= avg;
        k--;
        sum -= avg;
    }
 
    assert(sum == 0);
 
    returnmul;
}