Description

In this problem, you are given a pair of integers A and B. You can transform any integer number A to B by adding x to A.This x is an integer number which is a prime below A.Now,your task is to find the minimum number of transformation required to transform S to another integer number T.

Input

Input contains multiple test cases.Each test case contains a pair of integers S and T(0< S < T <= 1000) , one pair of integers per line.

Output

For each pair of input integers S and T you should output the minimum number of transformation needed as Sample output in one line. If it's impossible ,then print 'No path!' without the quotes.

Sample Input

5
7
3
4

Sample Output

Need 1 step(s)
No path!
//从A->B能转化的要求是存在一个质数x,使得x<A ,而且A+x == B 。
//首先我们可以用线性筛素法筛出所有的素数,然后就是bfs搜索就可以了。 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN = ; struct Node
{
int num ,d ;
Node() {};
Node(int a, int b):num(a),d(b) {}
};
bool prim[MAXN] ;
bool mark[MAXN] ;
int C , S ,T ; void isprim()
{
for(int i = ; i < MAXN ; i ++)
prim[i] = ;
for(int i = ; i < MAXN ; i ++)
if(prim[i])
for(int j = ; i * j < MAXN ; j ++)
prim[i * j] = ;
} int bfs(int source , int destation)
{
queue<Node> Q ;
memset(mark , , sizeof(mark)) ;
Node a ;
Q.push( Node(source , ) ) ;
mark[source] = ;
while( !Q.empty() )
{
a = Q.front() ;
Q.pop() ;
//搜索比它小的素数
for(int i = ; i < a.num ; i ++)
{
//判断要加的数是不是素数
if(!prim[i] )continue ; int number = a.num + i ;
//大于所要求的目标数,直接退出
if(number > destation) break ;
if(mark[number]) continue ; mark[number] = ;
if(number == destation)
return a.d + ;
Q.push( Node(number , a.d+) ) ;
}
}
return - ;
} int main()
{
isprim() ;
while( scanf("%d%d" , &S , &T) == )
{
int d = bfs(S , T) ;
if(d==-) printf("No path!\n") ;
else printf("Need %d step(s)\n" , d) ;
}
return ;
}

BFS

Number Transformation的更多相关文章

  1. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  2. bzoj 3858: Number Transformation 暴力

    3858: Number Transformation Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 82  Solved: 41[Submit][Sta ...

  3. HDU-4952 Number Transformation

    http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...

  4. CodeForces346 C. Number Transformation II

    C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. Codeforces 251C Number Transformation

    Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...

  6. LightOJ 1141 Number Transformation

    Number Transformation In this problem, you are given an integer number s. You can transform any inte ...

  7. CodeForces 346C Number Transformation II

    Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1,  (k+1)* x ...

  8. G - Number Transformation BFS

    In this problem, you are given an integer number s. You can transform any integer number A to anothe ...

  9. G - Number Transformation(BFS+素数)

    In this problem, you are given an integer number s. You can transform any integer number A to anothe ...

随机推荐

  1. Python Socket单线程+阻塞模式

    Python之旅]第五篇(二):Python Socket单线程+阻塞模式 python Socket单线程 Socket阻塞模式 串行发送 摘要:  前面第五篇(一)中的一个Socket例子其实就是 ...

  2. js的new操作符

    1.创建一个空对象,并且 this 变量引用该对象,同时还继承了该函数的原型. 2.属性和方法被加入到 this 引用的对象中. 3.新创建的对象由 this 所引用,并且最后隐式的返回 this . ...

  3. 【转】Java 读写Properties配置文件

    [转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...

  4. C#错误异常列表

    Exception: 所有异常对象的基类. SystemException:运行时产生的所有错误的基类. IndexOutOfRangeException:当一个数组的下标超出范围时运行时引发. Nu ...

  5. ASP.NET几种清除页面缓存的方法

    在asp.net中使用模式dialog时,你会发现每次打开的页面都是相同的内容,页面内容并没有刷新,这是缓存的原因造成的, 解决方法如下: 第一种是ASP.NET清除页面缓存 Response.Buf ...

  6. Linux svn一次增加多个文件并批量上传

    命令行下操作svn没有使用界面形式的TortoiseSVN直观,但是不管怎样,命令行下操作svn还是有它的有点,如果你碰到一次需要svn add许多个文件怎么办?下面的命令可以帮助你解决这个问题 一次 ...

  7. PLSQL远程连接到Oracle服务器

    这里只介绍一种远程连接服务器方法,即本机安装了Oracle客户端和PLSql工具,服务器安装在虚拟机或者另一台电脑上 1.打开Oracle客户端的Net Manager,选择Oracle Net配置— ...

  8. tomcat中有关配置文件的说明

    在以往的tomcat使用中本人一直都没有注意到tomcat的conf目录下配置文件的作用,都是"拿来主义"的思想,从未深究.但是最近遇到很多有关tomcat配置的问题,很是头大,所 ...

  9. 【USACO 1.3.1】混合牛奶

    [题目描述] 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助梅丽乳业找到最优的牛奶采购方案. 梅丽乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的. ...

  10. 推荐一个有趣的软件"Process Monitor"

    同事给的,用起来感觉很不错,官网地址:http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx 以下为官网介绍: Introducti ...