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+Jquery Mobile学习系列(1)-开发环境

    开发环境是老生常谈的问题了,网上有很多关于Android环境安装的文章,我这里也就简单说明一下,不做过多分析. 想了解详细的安装说明,可以参见[百度经验] Java环境安装直接跳过,说一下Androi ...

  2. sublime中BracketHighlighter 插件使用 (转)

    sublime中BracketHighlighter 插件使用 1.打开package Control,选择install Package 2.输入BracketHighlighter,回车 3.这样 ...

  3. HTML5动态时钟

    实现效果 源码可以去github下载 地址:https://github.com/feifeiliu/jsBlock 参考:慕课网动态时钟

  4. anaconda 使用 及 tensorflow-gpu 安装

    Anaconda简易使用 创建新环境 conda create -n rcnn python=3.6 删除环境 conda remove -n rcnn --all 进入环境 conda activa ...

  5. NOIP2013T1 转圈游戏 快速幂

    描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置, --, 依此 ...

  6. A - I Wanna Be the Guy

    Problem description There is a game called "I Wanna Be the Guy", consisting of n levels. L ...

  7. angular4搭建博客(一)

    本文长期更新,未经运行,严禁转载. 博客(制作中) http://101.200.58.228/ Github https://github.com/Teloi/TEIndex 框架选择 Angula ...

  8. 【Oracle】数据库热备

    1. 创建脚本 注:脚本第三行中的DB_NAME,需要改为自己的数据库名(show parameter name;): oracle用户下新建目录:/home/oracle/DB_NAME/hot_b ...

  9. Jetty容器配置https

    Configuring the Jetty Container as a Https Connector Jetty版本:9.2.22.v20170606 Pom.xml <?xml versi ...

  10. ObjectT5:在线随机森林-Multi-Forest-A chameleon in track in

    原文::Multi-Forest:A chameleon in tracking,CVPR2014  下的蛋...原文 使用随机森林的优势,在于可以使用GPU把每棵树分到一个流处理器里运行,容易并行化 ...