HDU 4394 Digital Square

Digital Square

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1882    Accepted Submission(s): 741


Problem Description
Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow condition: M 2%10 x=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 <=10 9), 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
 

 

Source
 
 
 
解析:设M = abc,即M = 100*a+10*b+c,则M 2 = 10000*a 2 + 1000*2*a*b + 100*(b 2+2*a*c) + 10*2*b*c + c 2。易知,M 2的个位只与M的最后1位有关,M 2的十位只与M的最后2位有关,M 2的百位只与M的最后3位有关……我们可以从个位开始进行广搜,逐位满足N。如果有可行解,找出这一层当中最小的解并输出;否则输出"None"。
 
 
 
 1 #include <cstdio>
 2 #include <queue>
 3 using namespace std;
 4 
 5 struct node{
 6     long long value,place;
 7 };
 8 
 9 void bfs(long long n)
10 {
11     node tmp;
12     tmp.value = 0;
13     tmp.place = 1;
14     queue<node> q;
15     q.push(tmp);
16     bool findans = false;
17     long long ans = 0xffffffff;
18     while(!q.empty()){
19         tmp = q.front();
20         q.pop();
21         node now;
22         now.place = tmp.place*10;
23         for(int i = 0; i<10; ++i){
24             now.value = tmp.value+i*tmp.place;
25             if(now.value*now.value%now.place == n%now.place){
26                 if(!findans)
27                     q.push(now);
28                 if(now.value*now.value%now.place == n && now.value<ans){
29                     findans = true;
30                     ans = now.value;
31                 }
32             }
33         }
34     }
35     if(ans == 0xffffffff)
36         printf("None\n");
37     else
38         printf("%I64d\n",ans);
39 }
40 
41 int main()
42 {
43     int t;
44     scanf("%d",&t);
45     while(t--){
46         long long n;
47         scanf("%I64d",&n);
48         bfs(n);
49     }
50     return 0;
51 }

 

上面的代码找到可行解之后需要进行比较找出最优解,我们可以对此进行优化,把queue改为priority_queue,让priority_queue帮我们完成这个工作,使得找到的第一个可行解便是满足题意的最优解。

 

 1 #include <cstdio>
 2 #include <queue>
 3 using namespace std;
 4 
 5 struct node{
 6     long long value,place;
 7     bool operator < (const node& b)const
 8     {
 9         return value>b.value;
10     }
11 };
12 
13 void bfs(long long n)
14 {
15     node tmp;
16     tmp.value = 0;
17     tmp.place = 1;
18     priority_queue<node> q;
19     q.push(tmp);
20     while(!q.empty()){
21         tmp = q.top();
22         q.pop();
23         if(tmp.value*tmp.value%tmp.place == n){
24             printf("%I64d\n",tmp.value);
25             return ;
26         }
27         node now;
28         now.place = tmp.place*10;
29         for(int i = 0; i<10; ++i){
30             now.value = tmp.value+i*tmp.place;
31             if(now.value*now.value%now.place == n%now.place){
32                 q.push(now);
33             }
34         }
35     }
36     printf("None\n");
37 }
38 
39 int main()
40 {
41     int t;
42     scanf("%d",&t);
43     while(t--){
44         long long n;
45         scanf("%I64d",&n);
46         bfs(n);
47     }
48     return 0;
49 }

 

转载于:https://www.cnblogs.com/inmoonlight/p/5245802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值