Digital Square 搜索

Digital Square
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow condition: M2%10x=N (x=0,1,2,3....)
 

Input

The first line has an integer T( T< = 1000), the number of test cases. 
For each case, each line contains one integer N(0<= N <=109), indicating the given number.
 

Output

For each case output the answer if it exists, otherwise print “None”.
 

Sample Input

3
3
21
25
 

Sample Output

None
11
5
 
 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <set>
 4 #include <string.h>
 5 #include <algorithm>
 6 #include <queue>
 7 using namespace std;
 8 #define ll long long
 9 ll n,nu,ans;
10 void init()
11 {
12     ll nn=n;
13     nu=1;
14     while(nn)
15     {
16         nn/=10;
17         nu*=10;
18     }
19 }
20 bool fun()
21 {
22     int i,ok=0;
23     ans=n;
24     ll now,x,y,noww;
25     queue<pair<ll,ll> >q;
26     while(!q.empty())
27     q.pop();
28     q.push(make_pair(0,1));
29     while(!q.empty())
30     {
31         x=q.front().first;
32         now=q.front().second;
33         noww=now*10;
34         q.pop();
35         for(i=0;i<10;i++)
36         {
37             y=x+now*i;
38             if(y*y%nu==n){ans=min(ans,y);ok=1;}
39             else
40             if(y*y%noww==n%noww)q.push(make_pair(y,noww));
41         }
42     }
43     if(ok)return 1;
44     return 0;
45 }
46 int main()
47 {
48     int t;
49     scanf("%d",&t);
50     while(t--)
51     {
52         cin>>n;
53         if(n==0)
54         {
55             cout<<10<<endl;
56             continue;
57         }
58         init();
59         if(fun())
60         {
61             cout<<ans<<endl;
62         }
63         else printf("None
");
64     }
65 }
View Code
 
原文地址:https://www.cnblogs.com/ERKE/p/3843998.html