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. Swift: Initialization-2

    Failable Initializers 有的时候,可能是参数问题.需要的外部资源没有到位等原因,初始化可能失败.为了应对这种情况,我们可以定义一个或多个可失败的构造方法. init? A fail ...

  2. select poll epoll三者之间的比较

    一.概述 说到Linux下的IO复用,系统提供了三个系统调用,分别是select poll epoll.那么这三者之间有什么不同呢,什么时候使用三个之间的其中一个呢? 下面,我将从系统调用原型来分析其 ...

  3. POJ 1986(LCA and RMQ)

    题意:给定一棵树,求任意两点之间的距离. 思路:由于树的特殊性,所以任意两点之间的路径是唯一的.u到v的距离等于dis(u) + dis(v) - 2 * dis(lca(u, v)); 其中dis( ...

  4. Electron

    跨平台桌面app开发 Appjs hex nwjs electron 官网:http://electron.atom.io/ 中文文档:https://github.com/atom/electron ...

  5. Web弹框类

    using System; using System.Text; namespace Core { /// <summary> /// MessageBox 的摘要说明. /// < ...

  6. Lock锁_线程_线程域

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. 安装PHP过程中,make步骤报错:(集合网络上各种解决方法)

    安装PHP过程中,make步骤报错:(集合网络上各种解决方法) (1)-liconv -o sapi/fpm/php-fpm /usr/bin/ld: cannot find -liconv coll ...

  8. 【USACO 2.4.5】分数化小数

    [描述] 写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式. 如果小数有循环节的话,把循环节放在一对圆括号中. 例如, 1/3 =0.33333333 写成0.(3), 4 ...

  9. 『重构--改善既有代码的设计』读书笔记----Extract Method

    在编程中,比较忌讳的一件事情就是长函数.因为长函数代表了你这段代码不能很好的复用以及内部可能出现很多别的地方的重复代码,而且这段长函数内部的处理逻辑你也不能很好的看清楚.因此,今天重构第一个手法就是处 ...

  10. jQuery中的综合动画

    所谓综合动画,就是在链式表达式依次执行相关animate函数,其中的参数是以键值对的方式存在的. 如下示例,就展示了一个基本的综合动画. <!DOCTYPE html PUBLIC " ...