http://acm.hdu.edu.cn/showproblem.php?pid=4394
题意:
M^2%10^x=N (x=0,1,2,3....),给你一个N,求M,x为0,1,2,3....其中一个数就行了。找不到M输出None
也就是求N是某个数的平方的后缀(包括本身)。
题解 :也就是求N是某个数的平方的后缀(包括本身)。
bfs 搜素每一种可能的情况 ,首先搜索 个位,然后十位 等等。。。。
1 #include<cstdio>
2 #include<cstring>
3 #include<cmath>
4 #include<iostream>
5 #include<algorithm>
6 #include<set>
7 #include<map>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #define Min(a,b) a<b?a:b
12 #define Max(a,b) a>b?a:b
13 #define CL(a,num) memset(a,num,sizeof(a));
14 #define maxn 110
15 #define eps 1e-6
16 #define inf 9999999
17 typedef __int64 ll;
18 using namespace std;
19 ll n;
20 struct node
21 {
22 ll k;
23 ll x;
24 node(ll a,ll b):k(a),x(b){}
25 friend bool operator < (node a,node b)
26 {
27 return a.k > b.k ;
28 }
29 };
30 ll f[20] ;
31 void init()
32 {
33 int i;
34 f[0] = 1;
35 for( i = 1 ; i<= 18;i++)
36 {
37 f[i] =f[i - 1]*10 ;
38 }
39 }
40 void bfs()
41 {
42 priority_queue<node>que;
43 que.push(node(0,0));
44 int i ;
45 while(!que.empty())
46 {
47
48 node a= que.top();que.pop();
49 ll x = a.x ;
50 ll k = a.k;
51 if(x>=18)continue ;
52
53 if((k*k) % f[x] == n)
54 {
55 printf("%I64d\n",k);
56 return ;
57 }
58
59 for( i = 0;i<=9;i++)//要从0 开始
60 {
61 ll tmp = i*f[x] + k;
62 if((tmp%f[x + 1])*(tmp%f[x + 1])%f[x+1] == n%f[x + 1] )//剪枝
63 que.push(node(tmp,x + 1));
64 }
65 }
66 printf("None\n");
67
68 }
69 int main()
70 {
71 int t;
72 init();
73 scanf("%d",&t);
74 while(t--)
75 {
76 scanf("%I64d",&n);
77 int tmp = n%10;
78 if(tmp == 2||tmp ==3||tmp == 7||tmp ==8)
79 {
80 printf("None\n");
81 continue ;
82 }
83 bfs();
84 }
85
86 }
2 #include<cstring>
3 #include<cmath>
4 #include<iostream>
5 #include<algorithm>
6 #include<set>
7 #include<map>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #define Min(a,b) a<b?a:b
12 #define Max(a,b) a>b?a:b
13 #define CL(a,num) memset(a,num,sizeof(a));
14 #define maxn 110
15 #define eps 1e-6
16 #define inf 9999999
17 typedef __int64 ll;
18 using namespace std;
19 ll n;
20 struct node
21 {
22 ll k;
23 ll x;
24 node(ll a,ll b):k(a),x(b){}
25 friend bool operator < (node a,node b)
26 {
27 return a.k > b.k ;
28 }
29 };
30 ll f[20] ;
31 void init()
32 {
33 int i;
34 f[0] = 1;
35 for( i = 1 ; i<= 18;i++)
36 {
37 f[i] =f[i - 1]*10 ;
38 }
39 }
40 void bfs()
41 {
42 priority_queue<node>que;
43 que.push(node(0,0));
44 int i ;
45 while(!que.empty())
46 {
47
48 node a= que.top();que.pop();
49 ll x = a.x ;
50 ll k = a.k;
51 if(x>=18)continue ;
52
53 if((k*k) % f[x] == n)
54 {
55 printf("%I64d\n",k);
56 return ;
57 }
58
59 for( i = 0;i<=9;i++)//要从0 开始
60 {
61 ll tmp = i*f[x] + k;
62 if((tmp%f[x + 1])*(tmp%f[x + 1])%f[x+1] == n%f[x + 1] )//剪枝
63 que.push(node(tmp,x + 1));
64 }
65 }
66 printf("None\n");
67
68 }
69 int main()
70 {
71 int t;
72 init();
73 scanf("%d",&t);
74 while(t--)
75 {
76 scanf("%I64d",&n);
77 int tmp = n%10;
78 if(tmp == 2||tmp ==3||tmp == 7||tmp ==8)
79 {
80 printf("None\n");
81 continue ;
82 }
83 bfs();
84 }
85
86 }