A14557 | Make a Power of Two
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ .
- 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$ .
输入格式
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$ ).
Each test case consists of one line containing one integer $n$ ( $1 \le n \le 10^9$ ).
输出格式
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$ .
输入输出样例
输入 #1
12 1052 8888 6 75 128 1 301 12048 1504 6656 1000000000 687194767
输出 #1
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 $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$ ).
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$ ).
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted