Make a Power of Two (Codeforces 1560D)
Tags: Math, strings

You are given an integer $$$n$$$. In $$$1$$$ move, you can do one of the following actions:  erase any digit of the number (it's acceptable that the number before the operation has exactly one digit and after the operation, it is "empty");  add one digit to the right. The actions may be performed in any order any number of times.Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number $$$301$$$ the digit $$$3$$$, the result is the number $$$01$$$ (not $$$1$$$).You need to perform the minimum number of actions to make the number any power of $$$2$$$ (i.e. there's an integer $$$k$$$ ($$$k \ge 0$$$) such that the resulting number is equal to $$$2^k$$$). The resulting number must not have leading zeroes.E.g. consider $$$n=1052$$$. The answer is equal to $$$2$$$. First, let's add to the right one digit $$$4$$$ (the result will be $$$10524$$$). Then let's erase the digit $$$5$$$, so the result will be $$$1024$$$ which is a power of $$$2$$$.E.g. consider $$$n=8888$$$. The answer is equal to $$$3$$$. Let's erase any of the digits $$$8$$$ three times. The result will be $$$8$$$ which is a power of $$$2$$$.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow.Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n \le 10^9$$$).

Output: For each test case, output in a separate line one integer $$$m$$$ — the minimum number of moves to transform the number into any power of $$$2$$$.

Note: The answer for the first test case was considered above.The answer for the second test case was considered above.In the third test case, it's enough to add to the right the digit $$$4$$$ — the number $$$6$$$ will turn into $$$64$$$.In the fourth test case, let's add to the right the digit $$$8$$$ and then erase $$$7$$$ and $$$5$$$ — the taken number will turn into $$$8$$$.The numbers of the fifth and the sixth test cases are already powers of two so there's no need to make any move.In the seventh test case, you can delete first of all the digit $$$3$$$ (the result is $$$01$$$) and then the digit $$$0$$$ (the result is $$$1$$$).

Sample Inputs:
12
1052
8888
6
75
128
1
301
12048
1504
6656
1000000000
687194767

Sample Outputs:
2
3
1
3
0
0
2
1
3
4
9
2

Solution (Python):
bi = [str(2 ** i) for i in range(55)]

def solve(a, b):
    l = len(a)
    l2 = len(b)
    if l > 2 * l:
        return l + 1
    j = 0
    for el in a:
        if el == b[j]:
            j += 1
        if j == l2:
            break
    return min(l + 1, l - 2 * j + l2)
for _ in range(int(input())):
    a = input()
    print(min([solve(a, x) for x in bi]))

Solution (Python):
def lcs(s1, p):
    j = 0
    matches = 0
    for c in p:
        j = s1.find(c, j) + 1
        if j == 0:
            break
        matches += 1
    return matches

def str_diff(s1, s2):
    intersection = lcs(s1, s2)
    return len(s1) + len(s2) - 2 * intersection
if __name__ == '__main__':
    powers = [str(2 ** i) for i in range(54)]
    n = int(input())
    for _ in range(n):
        x = input()
        print(min((str_diff(x, s) for s in powers)))

Solution (Python):
import copy
import math
from sys import stdin

def A():
    t = int(stdin.readline())
    while t:
        t -= 1
        cnt = 0
        k = int(stdin.readline())
        i = 0
        while True:
            if i % 3 == 0 or i % 10 == 3:
                i += 1
                continue
            cnt += 1
            if cnt == k:
                break
            i += 1
        print(i)

def B():
    t = int(stdin.readline())
    while t:
        t -= 1
        (a, b, c) = [int(x) for x in stdin.readline().split()]
        k = abs(b - a)
        k1 = c + k
        if k * 2 < max(a, b, c):
            print(-1)
        elif k1 > k * 2:
            k1 = c - k
            if k1 >= 0:
                print(k1)
            else:
                print(-1)
        else:
            print(k1)

def C():
    t = int(stdin.readline())
    while t:
        t -= 1
        a = [int(x) for x in stdin.readline().split()]
        k1 = copy.deepcopy(a[0])
        k = math.sqrt(a[0])
        if int(k) ** 2 == k1:
            print('{} {}'.format(int(k), 1))
        else:
            top = int(math.ceil(k))
            if top * top - top + 1 <= k1 <= top * top:
                print('{} {}'.format(top, top * top - k1 + 1))
            else:
                print('{} {}'.format(k1 - (top - 1) ** 2, top))

def D():
    t = int(stdin.readline())
    r = [str(2 ** i) for i in range(60)]

    def min_dif(x, s):
        cnt = 0
        for i in x:
            if i == s[cnt]:
                cnt += 1
                if cnt == len(s):
                    break
        return len(x) + len(s) - 2 * cnt
    for i in range(t):
        n = input()
        print(min([min_dif(n, i) for i in r]))

def F1():
    t = int(stdin.readline())
    while t:
        t -= 1
        (n, k) = input().split()
        n = list(n)
        k = int(k)
        if k == 1:
            for i in range(1, len(n)):
                if n[i] > n[0]:
                    n[0] = n[i]
            for i in n:
                print(n[0], end='')
        elif len(n) <= 2:
            for i in n:
                print(i, end='')
        else:
            l = 1
            n = list(n)
            for i in range(1, len(n)):
                if n[i] == n[0]:
                    l += 1
                else:
                    break
            if l >= len(n) - 1:
                for i in n:
                    print(i, end='')
            elif l < len(n) and n[l] < n[0]:
                for i in range(l, len(n)):
                    n[i] = n[0]
                for i in n:
                    print(i, end='')
            else:
                l1 = copy.deepcopy(l)
                if l1 >= len(n) - 1:
                    for i in n:
                        print(i, end='')
                    continue
                maxans = max(n[l1 + 1:-1])
                if maxans == n[l1]:
                    for i in range(l1, len(n)):
                        n[i] = n[l1]
                    for i in n:
                        print(i, end='')
                elif maxans <= n[0]:
                    for i in range(l1 + 1, len(n)):
                        n[i] = n[0]
                    for i in n:
                        print(i, end='')
                else:
                    for i in range(0, min(l1, len(n))):
                        print(n[0], end='')
                    if l1 < len(n):
                        print(ord(n[l1]) - ord('0') + 1, end='')
                        for i in range(l1 + 1, len(n)):
                            print(n[0], end='')
        print()
D()