Kattis之旅——Prime Path
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now
and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am
the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime.
You will just have to paste four new digits over the four old
ones on your office door.
— No, it’s not that simple. Suppose that I change the first
digit to an 8, then the number will read 8033 which is not a
prime!
— I see, being the prime minister you cannot stand having a
non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to
8179 by a path of prime numbers where only one digit is changed
from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping,
intervened.
— No unnecessary expenditure, please! I happen to know that the
price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the
cost. You don’t know some very cheap software gurus, do
you?
— In fact, I do. You see, there is this programming contest
going on…
Help the prime minister to find the cheapest prime path
between any two given four-digit primes! The first digit must
be nonzero, of course. Here is a solution in the case
above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6
pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1
must be purchased.
Input
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
Output
One line for each case, either with a number stating the minimal cost or containing the word “Impossible”.
Sample Input 1 | Sample Output 1 |
---|---|
3 |
6 |
大致意思就是由前面的那个素数变到后面的那个素数,每次只能变一位数,变化后的数也应该是一个素数(无论是不是所求的数),求变化次数。
直接BFS即可。
//Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
ll n, m, s, res, ans, len, T, k;
int x, y;
int pr[maxn]; int P(int n) {
for(int i=; i*i<=n; i++) {
if( n%i== ) return ;
}
return ;
}
//将k位化0
int change(int n, int k) {
char s[] = {};
sprintf(s, "%d", n);
s[k] = '';
sscanf(s, "%d", &n);
return n;
} int solve(int s, int e) {
queue<int> q;
int dis[maxn] = {};
q.push(s);
dis[s] = ;
while( q.size() ) {
s = q.front();
q.pop();
if( s == e ) return dis[s]-;
int t = ;
for(int i=; i<; i++) {
int k = change(s, i);
for(int j=; j<; j++) {
int a = k+j*t;
if( pr[a]== && dis[a]== ) {
q.push(a);
dis[a] = dis[s]+;
}
}
t /= ;
}
}
return -;
} void input() {
for(int i=; i<maxn; i++) pr[i] = P(i);
cin >> T;
while( T -- ) {
cin >> x >> y;
ans = solve(x, y);
if( ans==- ) puts("Impossible");
else cout << ans << endl;
}
} int main(){
input();
return ;
}
Kattis之旅——Prime Path的更多相关文章
- Kattis之旅——Prime Reduction
A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- Prime Path(素数筛选+bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9519 Accepted: 5458 Description The m ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- Prime Path(POJ 3126 BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15325 Accepted: 8634 Descr ...
随机推荐
- ABP中针对sql2008的数据库配置
- 快学Scala 2
控制结构和函数 1.在Scala中,几乎所有构造出来的语法结构都有值.这个特性是为了使得程序更加精简,也更易读. (1)if表达式有值 (2)块也有值——是它最后一个表达式的值 (3)Scala的fo ...
- 敏捷开发— —Scrum 学习笔记
敏捷开发模式是一种从1990年代开始逐渐引起广泛关注的一些新型软件开发方法,是一种应对快速变化的需求的一种软件开发能力.它们的具体名称.理念.过程.术语都不尽相同,相对于"非敏捷" ...
- nodejs, vue, webpack 项目实践
vue 及 webpack,均不需要与nodejs一期使用,他们都可以单独使用到任何语言的框架中. http://jiongks.name/blog/just-vue/ https://cn.vuej ...
- kickstart模式实现批量安装centos7.x系统
1.1 安装系统的方法 l 光盘(ISO文件,光盘的镜像文件)===>>每一台物理机都得给一个光驱,如果用外置光驱的话,是不是每台机器都需要插一下 l U盘:ISO镜像刻录到U盘==& ...
- 创建genil component
1: 创建一个类继承 CL_WCF_GENIL_ABSTR_COMPONENT 2:创建 genil _ editor 创建 component, 填入该实现类. 3: genil component ...
- 【Tools】-NO.4.Tools.1.VM.1.001-【VMware Workstation PRO 12 Install CentOS 7.1】-
1.0.0 Summary Tittle:[Tools]-NO.4.Tools.1.VM.1.001-[VMware Workstation PRO 12 Install CentOS 7.1]- S ...
- Centos的升级与更新
系统升级(6.5->7.2): 这里拿Centos6升级到Centos7为例: 1.查看当前CentOS版本cat /etc/redhat-release 2.更新源vim /etc/yum.r ...
- linux telnet命令
telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户 ...
- linux ~/ 和 /
/是目录层的分隔.表示符.只有一个/表明是root,/etc/表明是根目录下面的etc目录(当然目录最后不需要/,但有/直接表明他是目录,没有末尾的/,那么/etc需要检测一下确定是目录还是文件,虽然 ...