1560D.md (2436B)
1 Source: https://codeforces.com/problemset/problem/1560/D 2 3 D. Make a Power of Two 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an integer $n$. In $1$ move, you can do one of the following actions: 22 23 * 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"); 24 * add one digit to the right. 25 26 The actions may be performed in any order any number of times. 27 28 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$). 29 30 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. 31 32 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$. 33 34 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$. 35 36 Input 37 38 The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. 39 40 Each test case consists of one line containing one integer $n$ ($1 \le n \le 10^9$). 41 42 Output 43 44 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$. 45 46 Example 47 48 Input 49 50 ``` 51 12 52 1052 53 8888 54 6 55 75 56 128 57 1 58 301 59 12048 60 1504 61 6656 62 1000000000 63 687194767 64 ``` 65 66 Output 67 68 ``` 69 2 70 3 71 1 72 3 73 0 74 0 75 2 76 1 77 3 78 4 79 9 80 2 81 ``` 82 83 Note 84 85 The answer for the first test case was considered above. 86 87 The answer for the second test case was considered above. 88 89 In the third test case, it's enough to add to the right the digit $4$ — the number $6$ will turn into $64$. 90 91 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$. 92 93 The numbers of the fifth and the sixth test cases are already powers of two so there's no need to make any move. 94 95 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$).