M
M
mbcsoft2015-02-13 21:36:18
Python
mbcsoft, 2015-02-13 21:36:18

How to send emoticons for the minimum number of operations?

The guy decided to send N emoticons to his girlfriend.
After typing M emoticons, he thought that it would be faster to get N emoticons with a certain combination of the following operations:
1. Erase the last emoticon [operation - " D "]
2. Copy all emoticons [operation - " C "]
3. Paste emoticons from the clipboard [operation - " V "]
You need to help the guy cope with the task in the minimum number of operations.
Display the symbolic algorithm.
Example: N = 12; M=5;
Conclusion: DCVV
Explanation (5-1) = 4; 4 + 4 + 4 = 12;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Dugin, 2015-02-14
@mbcsoft

Python code for calculating "on the forehead" (brute force) and identifying patterns:

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
from itertools import product

def process(existing, required):
    for opcount in xrange(existing+required):
        for combo in product('dcv', repeat=opcount):
            buffer = 0
            smiles = existing
            data_y = [existing]
            for char in combo:
                if char == 'd':
                    smiles -= 1
                elif char == 'v':
                    smiles += buffer
                else:
                    buffer = smiles
                data_y.append(smiles)
            if smiles == required:
                data_x = xrange(opcount+1)
                plt.plot(data_x, data_y, linewidth=1)
                print required, data_y
                return ''.join(combo)
    return '-'

for required in xrange(2, 101):
    plt.clf()
    plt.grid(True)
    plt.title('Target: %s' % required)
    plt.autoscale(enable=True, axis='both', tight=False)
    for existing in xrange(1, (required//2)+1):
        result = process(existing, required)
    plt.savefig('{:02d}.png'.format(required), dpi=96, bbox_inches='tight')

The task is divided into 2:
1) If M >= N div 2, then you need to delete everything up to half, copy-paste, and, if necessary, delete 1 smiley.
2) If step 1 is not met, then you need to calculate the divisors of N (if N is prime, then take the next few numbers greater than N) and go down to the nearest largest divisor of N, which is less than M. Remove emoticons to fall into the horizontal levels observed in the pictures corresponding to divisors.
In the general case, the solution is not unique - for example, neighboring groups D and V can be mixed in any sequence: DDVVV = VVVDD = VDVDV = ...
N=12. For M=5 we see DCVV (purple line). Levels (dividers) - 6, 4, 3, 2, 1:
N=28:
N=29:
N=30:
N=71:
N=72:
PDF with all pictures:dugin.org/dropbox/toster_188303.pdf (11 Mb)
PS https://ru.wikipedia.org/wiki/Knapsack_Task

E
Eddy_Em, 2015-02-14
@Eddy_Em

ceil(12/5) = 3 -> Npastes=3-1=2
floor(12/3) = 4 -> copyOn = 4
12-4*2=4 -> Ndeletes = 5-4 = 1
Same with any other numbers. Let's say N=14, M=5:
ceil(14/5) = 3 -> Npastes = 2
floor(14/2) = 7 -> copyOn = 5 (since we can't catch up to seven)
14 - 5*2 = 4 -> Ndeletes = 1
Answer: CDVV
It's easy to figure out when to write DC and when to write CD.
Otherwise, in general, it turns out that I decided to do my homework for free!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question