题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4394

思路很巧妙,要找到m,可以这样思考,n的个位是有m的个位决定的,从0-9搜一遍,满足情况的话就继续搜索m的十位,这里的状态转移可以利用之前的m,因为m是在m的自身上增加的,这时,其实个位是已经满足情况了,而且,n的个位,十位,百位等等是很难单独取出来的,所以就直接取完后面的全部数字。

#include <stdio.h>
#include <queue>
#include <math.h> using namespace std; struct NUM
{
long long num;
int len;
bool operator < (const NUM &a) const
{
return a.num < num;
}
}; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
priority_queue<NUM> Q;
int n;
scanf("%d",&n); NUM a,next; a.len = ;
a.num = ;
Q.push(a);
bool flag = false;
while(!Q.empty())
{
a = Q.top();
Q.pop(); if(a.num*a.num%(int)pow(,a.len)==n)
{
flag = true;
break;
}
a.len++;
for(int i=; i<=; i++)
{
next = a;
next.num +=i*(int)pow(,a.len-);
//printf("%d %d\n",next.num,next.len);
//printf("%d %d\n",next.num*next.num%(int)pow(10,next.len),n%(int)pow(10,next.len));
if(next.num*next.num%(int)pow(,next.len)==n%(int)pow(,next.len))
Q.push(next);
}
}
if(flag)
printf("%d\n",a.num);
else printf("None\n");
}
return ;
}

HDU(4394),数论上的BFS的更多相关文章

  1. hdu 4394 Digital Square(bfs)

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

  2. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  3. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  4. hdu 1104 数论+bfs

    Remainder Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. HDU 4394 BFS

    M2%10x=N (x=0,1,2,3....) 给出N.找到最小的满足条件的M 因为:N的个位仅仅由M的个位决定.N十位由M的个位和十位决定,N的百位由M的个位十位百位决定.以此类推 全部从个位開始 ...

  6. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  7. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  8. HDU 3085 Nightmare Ⅱ(双向BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...

  9. HDU 1254 推箱子(BFS加优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others)    Me ...

随机推荐

  1. etcd介绍

    etcd是一个开源的.分布式的键值对数据存储系统,提供共享配置.服务的注册和发现. etcd与zookeeper相比算是轻量级系统.etcd的raft比zookeeper的paxos简单. 我们用et ...

  2. java——链表、链表栈 LinkedListStack、链表队列 LinkedListQueue

    LikedList: package Date_pacage; public class LinkedList<E> { public static void main(String[] ...

  3. flume failed to start agent because dependencies were not found in classpath

    FLUME_CLASSPATH=/root/flume/lib/ copied comon jar files from hadoop folder to the flume folder. cp / ...

  4. firewall 端口转发

    centos 7 使用背景:某次新购阿里云服务器安装nginx后配置80转8080的内部转发 systemctl status firewalld ---查看守护进程状态systemctl start ...

  5. CentOS7/Ubuntu Linux java jdk 环境变量配置

    [root@localhost sony]# tar -zxvf jdk-8u121-linux-x64.tar.gz [root@localhost sony]# .0_121 /usr/local ...

  6. instancemethod, staticmethod, classmethod & abstractmethod

    实例方法.静态方法.类方法.抽象方法 1.  Python中方法的工作方式(How methods work in Python) A method is a function that is sto ...

  7. jemeter的简单使用

    建立测试计划 启动jmeter后,jmeter会自动生成一个空的测试计划,用户可以基于该测试计划建立自己的测试计划. 添加线程组 一个性能测试请求负载是基于一个线程组完成的.一个测试计划必须有一个线程 ...

  8. secureCRT配置——针对Home、End无法使用

  9. ToDictionary写法

    把List集合转化成Dictionary public ActionResult Dimo() { Dictionary<string, Object> param = new Dicti ...

  10. C# 字节数组和十六进制字符串之间转换的另类写法

    今天从http://www.cnblogs.com/NanaLich/archive/2012/05/24/2516860.html看到的,记录下来 主要是XmlSerializationReader ...