zoukankan      html  css  js  c++  java
  • 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: 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
     
    Source
     
     
     
    解析:设M = abc,即M = 100*a+10*b+c,则M2 = 10000*a2 + 1000*2*a*b + 100*(b2+2*a*c) + 10*2*b*c + c2。易知,M2的个位只与M的最后1位有关,M2的十位只与M的最后2位有关,M2的百位只与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
    ");
    37     else
    38         printf("%I64d
    ",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
    ",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
    ");
    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 }
  • 相关阅读:
    Linux文件系统命令 cd
    Linux文件系统命令 cat
    正则表达式(.+?)与(.+)区别
    Linux文件系统命令 ls
    如何正确认识Docker Kubernetes 和 Apache Mesos
    基于Nutch Solr等基于搭建一体化的数据抓取平台
    ubuntu apache ssl配置
    机器学习入门
    docker 安全性问题
    数据工程师面试必备——Python与数据库的那些事
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5245802.html
Copyright © 2011-2022 走看看