Make a Power of Two solution codeforces
You are given an integer. In 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 the digit , the result is the number (not ).
You need to perform the minimum number of actions to make the number any power of (i.e. there’s an integer ( ) such that the resulting number is equal to ). The resulting number must not have leading zeroes.
E.g. consider. The answer is equal to . First, let’s add to the right one digit (the result will be ). Then let’s erase the digit , so the result will be which is a power of .
E.g. consider. The answer is equal to . Let’s erase any of the digits three times. The result will be which is a power of .
The first line contains one integer( ) — the number of test cases. Then test cases follow.
Each test case consists of one line containing one integer( ).
For each test case, output in a separate line one integer— the minimum number of moves to transform the number into any power of .
12 1052 8888 6 75 128 1 301 12048 1504 6656 1000000000 687194767
2 3 1 3 0 0 2 1 3 4 9 2
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— the number will turn into .
In the fourth test case, let’s add to the right the digitand then erase and — the taken number will turn into .
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(the result is ) and then the digit (the result is ).
Solution: Click here