素数路(prime)

题目描述

已知一个四位的素数,要求每次修改其中的一位,并且要保证修改的结果还是一个素数,还不能出现前导零。你要找到一个修改数最少的方案,得到我们所需要的素数。
例如把1033变到8179,这里是一个最短的方案:
1033
1733
3733
3739
3779
8779
8179
修改了6次。

输入

1行,两个四位的素数(没有前导零),表示初始数和目标数。

输出

一个数,表示最少的操作次数。如果不可能,输出“Impossible”。

样例输入

1033 8179

样例输出

6
分析:bfs,预处理四位素数;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,all,a[maxn],vis[maxn];
bool sushu(int p)
{
if(p<=)return false;
else if(p==)return true;
else if(p%==)return false;
for(int i=;i*i<=p;i+=)if(p%i==)return false;
return true;
}
void dfs()
{
queue<int>p;p.push(n);vis[n]=;
while(!p.empty())
{
int u=p.front(),v;
p.pop();
if(u==m)return;
for(int i=;i<=;i++)
{
v=u-u%+i;
if(!vis[v]&&a[v])p.push(v),vis[v]=vis[u]+;
}
for(int i=;i<=;i++)
{
v=u-u/%*+i*;
if(!vis[v]&&a[v])p.push(v),vis[v]=vis[u]+;
}
for(int i=;i<=;i++)
{
v=u-u/%*+i*;
if(!vis[v]&&a[v])p.push(v),vis[v]=vis[u]+;
}
for(int i=;i<=;i++)
{
v=u-u/*+i*;
if(!vis[v]&&a[v])p.push(v),vis[v]=vis[u]+;
}
}
}
int main()
{
int i,j,k,t;
scanf("%d%d",&n,&m);
for(int i=;i<=;i++)
if(sushu(i))a[i]=;
dfs();
if(vis[m])printf("%d\n",vis[m]-);
else puts("Impossible");
//system ("pause");
return ;
}

 
 

素数路(prime)的更多相关文章

  1. 素数路(prime) (BFS)

    问题 C: 素数路(prime) 时间限制: 1 Sec  内存限制: 64 MB提交: 8  解决: 5[提交][状态][讨论版] 题目描述 已知一个四位的素数,要求每次修改其中的一位,并且要保证修 ...

  2. UVa 1644 (筛素数 + 二分) Prime Gap

    题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n ...

  3. 素数(Prime)

    素数的判断: #include<math.h> bool IsPrime(int n) { ) return false; int sqr = (int)sqrt(1.0*n); ; i& ...

  4. 素数路径Prime Path POJ-3126 素数,BFS

    题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...

  5. [POJ268] Prime Distance(素数筛)

    /* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...

  6. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  7. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  8. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  9. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

随机推荐

  1. HDU - 5547 Sudoku(数独搜索)

    Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself ...

  2. C#中泛型默认关键字(default)详解

    我们在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T:(T 是引用类型还是值类型?)对此我们将如何处理? C#代码实例: /// <summary&g ...

  3. Chapter 1 First Sight——34

    "Was that the boy I sat next to in Biology?" I asked artlessly. 你是生物课坐在我旁边的男生吗?我天真烂漫的问道. & ...

  4. spark开发

    1. 主要参考资料http://spark.incubator.apache.org/docs/latest/scala-programming-guide.htmlhttp://www.eecs.b ...

  5. linux中的 tar命令的 -C 参数,以及其它一些参数(转)

    linux中的 tar命令的 -C 参数,以及其它一些参数 复制源:http://www.cnblogs.com/li-hao/archive/2011/10/03/2198480.htmltar命令 ...

  6. lldp中与snmp相关内容agentx

    struct lldpd { int    g_snmp; struct event  *g_snmp_timeout; void   *g_snmp_fds; char   *g_snmp_agen ...

  7. linuxmint卸载软件

    删除你已经卸载掉的软件包的命令为 sudo apt-get autoclean 还有一类软件包,我们每个人都应该删除,那就是你已经卸载了,但是一些只有它依赖而别的软件包都不需要的软件包还留在你的系统里 ...

  8. 拓扑序+dp Codeforces Round #374 (Div. 2) C

    http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...

  9. perl中my和our的区别分析

    来源: http://www.jb51.net/article/35528.htm perl中our的用法require 5.006当版本号小于 5.006 的时候,会返回失败,从而导致模块加载失败. ...

  10. perl的package和module

    来源: http://www.cnblogs.com/itech/archive/2010/03/23/1692836.html 一 package 1) package 相当于C++中的namesp ...