There's a dynamic programming solution. We start off knowing 0 keys can make us 0 A's. Then we iterate through for i
up to n
, doing two things: pressing A once and pressing select all + copy followed by paste j
times (actually j-i-1
below; note the trick here: the contents are still in the clipboard, so we can paste it multiple times without copying each time). We only have to consider up to 4 consecutive pastes, since select, copy, paste x 5 is equivalent to select, copy, paste, select, copy, paste and the latter is better since it leaves us with more in the clipboard. Once we've reached n
, we have the desired result.
The complexity might appear to be O(N), but since the numbers grow at an exponential rate it is actually O(N2) due to the complexity of multiplying the large numbers. Below is a Python implementation. It takes about 0.5 seconds to calculate for N=50,000.
def max_chars(n):
dp = [0] * (n+1)
for i in xrange(n):
dp[i+1] = max(dp[i+1], dp[i]+1) # press a
for j in xrange(i+3, min(i+7, n+1)):
dp[j] = max(dp[j], dp[i]*(j-i-1)) # press select all, copy, paste x (j-i-1)
return dp[n]
In the code, j
represents the total number of keys pressed after our new sequence of keypresses. We already have i
keypresses at this stage, and 2 new keypresses go to select-all and copy. Therefore we're hitting paste j-i-2
times. Since pasting adds to the existing sequence of dp[i]
A
's, we need to add 1
making it j-i-1
. This explains the j-i-1
in the 2nd-last line.
Here are some results (n
=> number of A's):
- 7 => 9
- 8 => 12
- 9 => 16
- 10 => 20
- 100 => 1391569403904
- 1,000 => 3268160001953743683783272702066311903448533894049486008426303248121757146615064636953144900245
174442911064952028008546304
- 50,000 => a very large number!
I agree with @SB that you should always state your assumptions: Mine is that you don't need to paste twice to double the number of characters. This gets the answer for 7, so unless my solution is wrong the assumption must be right.
In case someone wonders why I'm not checking sequences of the form Ctrl+A, Ctrl+C, A, Ctrl+V: The end result will always be the same as A, Ctrl+A, Ctrl+C, Ctrl+V which I do consider.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…