ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #739 (Div. 3)--D

2021-09-06 17:32:08  阅读:172  来源: 互联网

标签:case digit -- number will test 739 Div any


题干:

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≥0) such that the resulting number is equal to 2k2k). 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.

Input

The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.

Each test case consists of one line containing one integer nn (1≤n≤1091≤n≤109).

Output

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.

Example input Copy
12
1052
8888
6
75
128
1
301
12048
1504
6656
1000000000
687194767
output Copy
2
3
1
3
0
0
2
1
3
4
9
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 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).

**思路:**

处理出2的n次幂t,将输入数字和t都在转成字符串匹配.(注意:n要尽可能的大)

代码:

int main(){
    int t;
    cin>>t;string s,temp;
    while(t--){
        cin>>s; int ans=20;
        for(int i=0;i<=60;i++){
            temp=to_string(1ll<<i);int res=0;
            int k=0;
            for(int j=0;j<s.size();j++){
                if(k<temp.size()&&s[j]==temp[k])k++;
                else res++;
            }
            res+=temp.size()-k;
            ans=min(ans,res);
        }
        cout<<ans<<endl;
    }
    return 0;
}

标签:case,digit,--,number,will,test,739,Div,any
来源: https://www.cnblogs.com/muscletear/p/15160645.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有