Number Transformation
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
Sample Output
//从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的更多相关文章
- hdu4952 Number Transformation (找规律)
2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...
- bzoj 3858: Number Transformation 暴力
3858: Number Transformation Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 82 Solved: 41[Submit][Sta ...
- HDU-4952 Number Transformation
http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...
- CodeForces346 C. Number Transformation II
C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces 251C Number Transformation
Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...
- LightOJ 1141 Number Transformation
Number Transformation In this problem, you are given an integer number s. You can transform any inte ...
- CodeForces 346C Number Transformation II
Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1, (k+1)* x ...
- G - Number Transformation BFS
In this problem, you are given an integer number s. You can transform any integer number A to anothe ...
- G - Number Transformation(BFS+素数)
In this problem, you are given an integer number s. You can transform any integer number A to anothe ...
随机推荐
- js select 实现左右传值.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- POJ 2891 扩展欧几里德
这个题乍一看跟剩余定理似的,但是它不满足两两互素的条件,所以不能用剩余定理,也是给了一组同余方程,找出一个X满足这些方程,如果找不到的话就输出-1 因为它不满足互素的条件,所以两个两个的合并,最后合成 ...
- Apache POI解析excel文件
这里需要用到poi.jar和poi-ooxml.jar 没有的可以去http://mvnrepository.com/下载 import org.apache.poi.POIXMLDocument; ...
- ASP.NET-FineUI开发实践-13(二)
1.加图标和事件 上次已经通过DataSimulateTreeLevelField属性控制了树节点的显示,不用看也知道就是给指定列数据前面加个图标的HTML 可以在SimulateTreeHeper类 ...
- 应用app首次进入导航页动画
import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActi ...
- HTML5 File 对象
实例说明1: <div class="container"> <input type="file" id="file" m ...
- webconfig的设置节点几个说明
有助于深入理解webconfig <?xml version="1.0" encoding="utf-8" ?> <configuration ...
- RoadTrip 学习笔记
#RoadTrip 学习笔记 本篇是在Cmd Markdown中写完粘贴来的. RoadTrip介绍 RoadTrip 项目地址:https://github.com/romainguy/road-t ...
- web.xml配置文件中<async-supported>true</async-supported>报错的解决方案
为什么用到这个: ssh集成了cxf,当登录系统后,发现系统报错,控制台不断输出下面信息: 2016-05-05 11:05:06 - [http-bio-8080-exec-4] - WARN - ...
- Delphi 用ToolButton和MonthCalendar实现DateTimePicker的功能
效果图如下: 实现平台:xp xe2,其中以上功能的实现,核心主要是参考了万一老师的资料,连接:http://www.cnblogs.com/del/archive/2011/05/12/204411 ...