CF1560D.Make a Power of Two

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an integer nn. In 11 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 301301 the digit 33, the result is the number 0101 (not 11).

You need to perform the minimum number of actions to make the number any power of 22 (i.e. there's an integer kk (k≥0k \ge 0) such that the resulting number is equal to 2k2^k). The resulting number must not have leading zeroes.

E.g. consider n=1052n=1052. The answer is equal to 22. First, let's add to the right one digit 44 (the result will be 1052410524). Then let's erase the digit 55, so the result will be 10241024 which is a power of 22.

E.g. consider n=8888n=8888. The answer is equal to 33. Let's erase any of the digits 88 three times. The result will be 88 which is a power of 22.

给你一个整数 nn。在一次操作中,你可以执行以下任意一种操作:

  • 删除该数字的任意一位(允许操作前数字仅有一位,且操作后数字“为空”);
  • 在数字右侧添加一位数字。

上述操作可以以任意顺序执行任意多次。

注意:如果从某个数字中删除某一位后,结果包含前导零,则这些前导零不会被自动删除。例如,若从数字 301301 中删除数字 33,结果为数字 0101(而非 11)。

你需要执行最少次数的操作,使得最终得到的数字是某个 22 的幂(即存在整数 kk(k≥0k \ge 0),使得最终数字等于 2k2^k)。最终数字不得包含前导零。

例如,考虑 n=1052n=1052。答案为 22。首先,在右侧添加一位数字 44(结果为 1052410524);然后删除数字 55,结果为 10241024,这是一个 22 的幂。

再例如,考虑 n=8888n=8888。答案为 33。连续三次删除任意一个数字 88,结果为 88,这是一个 22 的幂。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Then tt test cases follow.

Each test case consists of one line containing one integer nn (1≤n≤1091 \le n \le 10^9).

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。随后是 tt 个测试用例。

每个测试用例由一行组成,其中包含一个整数 nn(1≤n≤1091 \le n \le 10^9)。

输出格式

For each test case, output in a separate line one integer mm — the minimum number of moves to transform the number into any power of 22.

对于每个测试用例,在单独一行中输出一个整数 mm —— 将该数变为任意一个 22 的幂所需的最少操作次数。

输入输出样例

  • 输入#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 44 — the number 66 will turn into 6464.

In the fourth test case, let's add to the right the digit 88 and then erase 77 and 55 — the taken number will turn into 88.

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 33 (the result is 0101) and then the digit 00 (the result is 11).

第一个测试用例的答案已在上文讨论。

第二个测试用例的答案已在上文讨论。

在第三个测试用例中,只需在右侧添加数字 44 —— 数字 66 将变为 6464。

在第四个测试用例中,我们先在右侧添加数字 88,然后删除 77 和 55 —— 所得数字将变为 88。

第五和第六个测试用例中的数字本身已是 2 的幂,因此无需进行任何操作。

在第七个测试用例中,你可以首先删除数字 33(结果为 0101),然后再删除数字 00(结果为 11)。

输入解题思路,AI测评打分。不知道怎么写?

首页