素数路(prime)
素数路(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)的更多相关文章
- 素数路(prime) (BFS)
问题 C: 素数路(prime) 时间限制: 1 Sec 内存限制: 64 MB提交: 8 解决: 5[提交][状态][讨论版] 题目描述 已知一个四位的素数,要求每次修改其中的一位,并且要保证修 ...
- UVa 1644 (筛素数 + 二分) Prime Gap
题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n ...
- 素数(Prime)
素数的判断: #include<math.h> bool IsPrime(int n) { ) return false; int sqr = (int)sqrt(1.0*n); ; i& ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- poj 1811 Prime Test 大数素数测试+大数因子分解
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 27129 Accepted: 6713 Case ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
随机推荐
- MFC利用ADO建立access数据源 ---包括访问带access密码与不带access密码两种方式)
void CDlg_login::OnButton1() { CString c_user,c_password;m_user1.GetWindowText(c_user);m_password1.G ...
- Unity3d之将terrain转化成mesh
Unity3d中,terrain还是比较耗的,DrawCall数也比较多,为了优化性能,可能需要将terrain转化成mesh. 现提供一工具,思路是根据terrain高度图生成mesh等,可参考: ...
- WebViewJavascriptBridge详细使用
前言 WebViewJavascriptBridge是支持到iOS6之前的版本的,用于支持native的iOS与javascript交互.如果需要支持到iOS6之前的app,使用它是很不错的.本篇讲讲 ...
- getTime 方法
转自http://www.yesky.com/imagesnew/software/javascript/html/jsmthgettime.htm getTime 方法返回 Date 对象中的时间值 ...
- Entity Framework技巧系列之十 - Tip 37 - 41
提示37. 怎样进行按条件包含(Conditional Include) 问题 几天前有人在StackOverflow上询问怎样进行按条件包含. 他们打算查询一些实体(比方说Movies),并且希望预 ...
- ueditor浏览器 无法上传文件.问题
dll也都引用了 路径绝对tmd没问题 最后 我一点一点的调试发现了问题 草tmd百度程序员 */UE.ajax = function() { //创建一个ajaxRequest对象 var fnSt ...
- MVC4数据注解和验证
model中的验证注解特性: public class StuInfo { public int ID { get; set;} [Display(Name = "姓名")] // ...
- sysbench使用教程【转载】
水晶命匣 2016-08-16 20:02 一.环境描述 此次使用的虚拟机环境如下所示: CPU:双核 2.4GHz 内存:4 GB 硬盘:120 GB IP:192.168.21.129 操作系统: ...
- 搭建Ubuntu下c/c++编译环境【转】
1. 安装Ubuntu. 2. 安装gcc 方法一: sudo apt-get install build-essential 安装完了可以执行 gcc--version的 ...
- Hibernate 系列教程12-继承-Join策略
Employee public class Employee { private Long id; private String name; HourlyEmployee public class H ...