M2%10x=N
(x=0,1,2,3....)

给出N。找到最小的满足条件的M

因为:N的个位仅仅由M的个位决定。N十位由M的个位和十位决定,N的百位由M的个位十位百位决定。以此类推

全部从个位開始搜索满足条件的数字就可以

#include"stdio.h"
#include "string.h"
#include "math.h"
#include "queue"
using namespace std;
__int64 flag,n; __int64 make(__int64 x,__int64 dit,__int64 num,__int64 i)
{
__int64 y,now,j;
y=1;
for (j=1;j<dit;j++)
y*=10; now=x+y*i;
if ((now*now%(y*10)/y)==num) return now; return -1;
}
void bfs()
{
queue<__int64>q;
__int64 dit,i,cnt,x,now,num;
q.push(0);
dit=0;
flag=-1;
while (!q.empty())
{
cnt=q.size();
num=n%10;
dit++;
n/=10;
while (cnt--)
{
x=q.front();
q.pop();
for (i=0; i<=9; i++)
{
now=make(x,dit,num,i); // x之前所生成的数字,dit当前搜索到第几位,应该位应该匹配的数字,搜索当前位数字为i
if (now!=-1)
{
q.push(now);
if (n==0)
{
if (now<flag|| flag==-1)
flag=now;
}
}
}
}
if (flag!=-1) return ; }
}
int main()
{
__int64 t;
scanf("%I64d",&t);
while (t--)
{
scanf("%I64d",&n);
if (n==0)
{
printf("0\n");
continue;
}
bfs();
if (flag==-1) printf("None\n");
else printf("%I64d\n",flag); }
return 0;
}

HDU 4394 BFS的更多相关文章

  1. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

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

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

  3. hdu 4394 Digital Square(bfs)

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

  4. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  5. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  6. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  7. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  8. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

  9. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

随机推荐

  1. (转)Android开发书籍推荐:从入门到精通系列学习路线书籍介绍

    Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 转自:http://blog.csdn.net/findsafety/article/details/52317506 很多时候我们都会不 ...

  2. Maven与IDEA结合

    转自:https://blog.csdn.net/zzpzheng/article/details/49474671 1. 什么是 Maven,为什么要使用 Maven 而不是 Ant Maven简单 ...

  3. LayoutInflater源码解析

    Android使用LayoutInflater来进行布局加载,通常获取方式有两种: 第一种: LayoutInflater layoutInflater = LayoutInflater.from(c ...

  4. 将我们的parser转换成Monad

    还记得我们上一篇delegate类型的parser吗 ,在开始本篇之前,强烈建议你复习一下这个parser定义  public delegate Maybe<Tuple<T,string& ...

  5. Java数组操作工具

    原文地址:http://blog.csdn.net/qq446282412/article/details/8913690 2013-05-11 10:27   看到网上的一段关于对数组操作的代码,觉 ...

  6. 【Oracle】删除undo表空间时,表空间被占用:ORA-30042: Cannot offline the undo tablespace

    特别注意:此办法只用于实在没有办法的时候,因为需要加入oracle中的隐含参数,慎用!!! 1. 先查一下是什么在占用undo SYS@ENMOEDU>select segment_name,o ...

  7. Outlook2010规则:尝试操作失败,找不到某个对象

    可以尝试通过清除规则的方法 启动 Outlook 并删除基于客户端的规则:outlook /cleanclientrules 如果失败,再执行这句 启动 Outlook 并删除基于服务器端的规则:ou ...

  8. 离线安装ADT和sdk

    重装Eclipse.离线安装ADT.Android SDK 由于最新的ADT.Android SDK需要最新版本的Eclipse才能使用,我无奈的只好升级Eclipse.看看自己的Eclipse已经两 ...

  9. (转) RabbitMQ学习之spring整合发送异步消息

    http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...

  10. sqlserver 分组 group by

    select 名称, COUNT(名称) as 数量之和from 信息group by all 名称