codeforces-962-c

题意:给你一个数,问从中删除某几位数字后重新组成的数字是否是某个数的平方;

解题思路:数据小,dfs直接搜,每位数只有两种选择,要或者不要

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
ll a[20];
int ans=999999;
int cnt;
void dfs(int step,ll x,int len)
{
    if(step>cnt)
        return;
    int m=sqrt(x);
    int tx=x;
    if(m*m==x&&x!=0)
    {
        int t=0;
        while(tx)
        {
            t++;
            tx=tx/10;
         }
        ans=min(ans,cnt-t);
    }
    dfs(step+1,x,len);
    dfs(step+1,x+a[step+1]*pow(10,len),len+1);
}
int main()
{
    ll n;
    ll z;
    cnt=0;
    cin>>n;
    z=n;
    int q=sqrt(n);
    if(q*q==n)
        cout<<"0\n";
    else
    {
 
    while(z)
    {
        cnt++;
        a[cnt]=z%10;
        z=z/10;
    }
    dfs(0,0,0);
    if(ans==999999)
        cout<<"-1\n";
    else
        cout<<ans<<endl;
    }
}

  

posted @   荒岛的龟  阅读(246)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示