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"。
 
 
 
 #include <cstdio>
#include <queue>
using namespace std; struct node{
long long value,place;
}; void bfs(long long n)
{
node tmp;
tmp.value = ;
tmp.place = ;
queue<node> q;
q.push(tmp);
bool findans = false;
long long ans = 0xffffffff;
while(!q.empty()){
tmp = q.front();
q.pop();
node now;
now.place = tmp.place*;
for(int i = ; i<; ++i){
now.value = tmp.value+i*tmp.place;
if(now.value*now.value%now.place == n%now.place){
if(!findans)
q.push(now);
if(now.value*now.value%now.place == n && now.value<ans){
findans = true;
ans = now.value;
}
}
}
}
if(ans == 0xffffffff)
printf("None\n");
else
printf("%I64d\n",ans);
} int main()
{
int t;
scanf("%d",&t);
while(t--){
long long n;
scanf("%I64d",&n);
bfs(n);
}
return ;
}

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

 #include <cstdio>
#include <queue>
using namespace std; struct node{
long long value,place;
bool operator < (const node& b)const
{
return value>b.value;
}
}; void bfs(long long n)
{
node tmp;
tmp.value = ;
tmp.place = ;
priority_queue<node> q;
q.push(tmp);
while(!q.empty()){
tmp = q.top();
q.pop();
if(tmp.value*tmp.value%tmp.place == n){
printf("%I64d\n",tmp.value);
return ;
}
node now;
now.place = tmp.place*;
for(int i = ; i<; ++i){
now.value = tmp.value+i*tmp.place;
if(now.value*now.value%now.place == n%now.place){
q.push(now);
}
}
}
printf("None\n");
} int main()
{
int t;
scanf("%d",&t);
while(t--){
long long n;
scanf("%I64d",&n);
bfs(n);
}
return ;
}

HDU 4394 Digital Square的更多相关文章

  1. hdu 4394 Digital Square(bfs)

    Digital Square Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. Digital Square(hdu4394)搜索

    Digital Square Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. Digital Square 搜索

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

  4. hdu Digital Square(广搜)

    题目:给出n,求出最小的m,满足m^2  % 10^k = n,其中k=0,1,2 http://acm.hdu.edu.cn/showproblem.php?pid=4394 只要有一个x满足条件便 ...

  5. HDU 1013 Digital Roots【字符串,水】

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. HDU 1013 Digital Roots(to_string的具体运用)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...

  7. HDU(4394),数论上的BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4394 思路很巧妙,要找到m,可以这样思考,n的个位是有m的个位决定的,从0-9搜一遍,满足情况的话就继 ...

  8. Hdu 1404 Digital Deletions

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1404 刚开始想采取找规律的方法解题,可以没有发现规律.无奈,只好采用求PN点的方法. 我们假 ...

  9. HDU 1013 Digital Roots(字符串)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

随机推荐

  1. Windows.Andy.Code4App.dll Win8.1/WP8.1通用类库@ver1.0.0

    直接入题! Win8.1和WP8.1眼下已经渐渐融为一体,WP8.1不断向Win8.1靠拢,虽然一些方法上WP8.1和Win8.1不同(ps:WP8.1和Win8.1的不同之处),但大部分还是相同的. ...

  2. Delphi XE5 如何设计并使用FireMonkeyStyle(转)

    如何设计并使用FireMonkeyStyle FireMonkey使用Style来控制控件的显示方式. 每个控件都有一个StyleLookup属性,FireMonkey就是通过控件的这个属性来在当前窗 ...

  3. ExtJS4.2学习(20)动态数据表格之前几章总结篇1(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2014-02-18/196.html --------------- ...

  4. CSRF注入式攻击防御讲解

    0x01 什么是CSRF攻击 CSRF是Cross Site Request Forgery的缩写(也缩写为XSRF),直译过来就是跨站请求伪造的意思,也就是在用户会话下对某个CGI做一些GET/PO ...

  5. ural 1221

    本来就是个很水的题  就是枚举起点长度然后直接判断就行了   但是比赛的时候写了个大bug 还找不出来     自己太水了 #include <cstdio> #include <c ...

  6. use sql trigger call java function

    Use UDF sys_exec to do this. You can use this link to use sys_exec function. It says, sys_exec sys_e ...

  7. POJ2031Building a Space Station

    http://poj.org/problem?id=2031 题意:你是空间站的一员,太空里有很多球形且体积不一的“小房间”,房间可能相距不近,也可能是接触或者甚至是重叠关系,所有的房间都必须相连,这 ...

  8. 学点PYTHON基础的东东--数据结构,算法,设计模式---观察者模式

    按照小明明的设计模式抄抄看看.. http://dongweiming.github.io/python-observer.html # 这个是观察者基类 class Subject(object): ...

  9. 关于PHP写APP接口的安全问题探讨(一)

    在探讨这个问题之前,先要确认一点的是,作为一名互联网Coder,无论你是前端或者后端你都要对http请求要有一定的了解,知道http特性,要清楚的了解http里面的Request与Response是什 ...

  10. winform学习日志(十九)----------真正三层架构之登录

    摘要:一:三层构架的基础知识在项目开发的过程中,有时把整个项目分为三层架构,其中包括:表示层(UI).业务逻辑层(BLL)和数据访问层(DAL).三层的作用分别如下: 表示层:为用户提供交互操作界面, ...