Prime Path
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10344 | Accepted: 5907 |
Description
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.
— 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.
1033The 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.
1733
3733
3739
3779
8779
8179
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
3 1033 8179 1373 8017 1033 1033
Sample Output
6 7 0
题意:
给出 N (<= 100)个 case,后每个 case 都给出两个四位数 A 和 B ,问最少需要将 A 经过多少次变化才能变成 B,每次变化只能变一位数,且每次得出来的数都必须为素数。
思路:
数学 + 搜索。首先先预处理好素数,再保存1000 ~ 9999之间的素数,并每个素数都对应着一个编号,并且转化成图,图用邻接表保存,每个数都有一个出度,指向的是与其相差一位的数。后给出 A 和 B 就是寻找 A 到 B 的最短路,故用 BFS 找最短路即可,若不预处理事先转化成素数图,在 BFS 中再一个个找的话,无疑会 TLE 的。
AC:
#include <cstdio> #include <queue> #include <map> #include <string.h> #define MAX 10000 using namespace std; typedef struct { int num,step; }node; node no[MAX]; int pri[MAX],pri_num[MAX]; int fir[MAX],next[MAX * 2],v[MAX * 2]; int vis[MAX]; int pri_sum,ind; void add_edge(int f,int t) { v[ind] = t; next[ind] = fir[f]; fir[f] = ind; ind++; } bool test(int a,int b) { int sum = 0; while(a && b) { if(a % 10 != b % 10) sum++; if(sum >= 2) return false; a /= 10; b /= 10; } return true; } void solve() { memset(pri,0,sizeof(pri)); memset(fir,-1,sizeof(fir)); pri_sum = 0; ind = 0; pri[1] = 1; for(int i = 2;i * i < MAX;i++) { if(pri[i]) continue; for(int j = i;j * i < MAX;j++) pri[i * j] = 1; } for(int i = 1000;i <= 9999;i++) if(!pri[i]) pri_num[pri_sum++] = i; for(int i = 0;i < pri_sum;i++) for(int j = i + 1;j < pri_sum;j++) { if(test(pri_num[i],pri_num[j])) { add_edge(i,j); add_edge(j,i); } } } int BFS(int st,int en) { if(st == en) return 0; queue<node> q; memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); node now; now.num = st;now.step = 0; q.push(now); vis[st] = 1; while(!q.empty()) { now = q.front();q.pop(); int num = now.num,step = now.step; //printf("%d %d\n",pri_num[num],step); for(int e = fir[num];e != -1;e = next[e]) { int vn = v[e]; if(!vis[vn]) { node in; in.num = vn;in.step = step + 1; if(vn == en) return in.step; vis[vn] = 1; q.push(in); } } } return -1; } int main() { int n; solve(); scanf("%d",&n); while(n--) { int s,e,res; scanf("%d%d",&s,&e); for(int i = 0;i < pri_sum;i++) { if(pri_num[i] == s) s = i; if(pri_num[i] == e) e = i; } res = BFS(s,e); if(res == -1) printf("Impossible\n"); else printf("%d\n",res); } return 0; }
相关推荐
java代码-使用java解决输出1000以内最大的n个质数及其和。输出形式“质数1+质数2+...+质数n=的源代码 ——学习参考资料:仅用于个人学习使用!
pi+素数.py
圆位置判定+质数判定+整根式化简三个功能,VB做得。。本人是新手,所以技术可能不太过关,偶尔会有出错的时候,希望高手帮忙优化下,谢谢!
编制具有如下原型的函数prime,用来判断整数n是否为素数:bool prime(int n); 而后编制主函数,任意输入一个大于4的偶数d,找出满足d=d1+d2的所有数对,其中要求d1与d2均为素数(通过调用prime来判断素数)。如偶数...
随机数+素数。随机生成n个数,并以列表形式输出,再输出其中的素数。.py
三-素数问题 6 素数性质 6 素数猜想 6 素数测试 6 筛素数 7 区间筛素数 7 大素数测试 7 素因子相关 8 梅森素数 8 筛可以表示成x^2+(x+1)^2的素数 9 高斯整数环与高斯素数 10 n!中素数y的个数 10 筛1~n的因子个数O(n)...
而后编制主函数,任意输入一个大于4的偶数d,找出满足d=d1+d2的所有数对,其中要求d1与d2均为素数(通过调用prime来判断素数)。如偶数18可以分解为11+7以及13+5;而偶数80可以分解为:43+37、61+19、67+13、73+7。...
资源名:MATLAB寻找素数的源程序代码_prime_number_素数_素数寻找_matlab 资源类型:matlab项目全套源码 源码说明: 全部项目源码都是经过测试校正后百分百成功运行的,如果您下载后不能运行可联系我进行指导或者...
PrimeNumber 素数生成器 V6.0.0.3 18.4KB 可快速生成指定范围内的所有素数,并可格式化输出;还可对单个自然数快速因数分解。 HugeCalc V6.x 以上版本现已提供该程序相应导出接口,欢迎使用。 若借助算法库 ...
超高速的素数筛选法,大数下10^6区间内筛选所有素数仅需30MS!
使用AKS算法检测素数和生成素数. 提供了AKS的6个步骤的方法 绝对原创
最短路径(单源dijkstra_bfs邻接表形式) 最短路径(单源dijkstra_bfs正向表形式) 最短路径(单源dijkstra+binary_heap邻接表形式) 最短路径(单源dijkstra+binary_heap正向表形式) 最短路径(单源dijkstra+mapped_...
这是我个人对原始素数筛选法的一个优化方法,效率提高一倍以上。 (文档内的代码为C/C++代码)
最短路径(单源dijkstra_bfs邻接表形式) 最短路径(单源dijkstra_bfs正向表形式) 最短路径(单源dijkstra+binary_heap邻接表形式) 最短路径(单源dijkstra+binary_heap正向表形式) 最短路径(单源dijkstra+mapped_...
最短路径(单源dijkstra_bfs邻接表形式) 最短路径(单源dijkstra_bfs正向表形式) 最短路径(单源dijkstra+binary_heap邻接表形式) 最短路径(单源dijkstra+binary_heap正向表形式) 最短路径(单源dijkstra+mapped_...
这是我个人对原始素数筛选法的一个优化方法,效率提高一倍以上。
2. 最短路径(单源dijkstra_bfs邻接表形式) 35 3. 最短路径(单源dijkstra_bfs正向表形式) 35 4. 最短路径(单源dijkstra+binary_heap邻接表形式) 36 5. 最短路径(单源dijkstra+binary_heap正向表形式) 37 6. 最短路径...
2015年春人教版五年级数学下册质数和合数.ppt
人教版五年级数学下册质数和合数 (2).ppt
而后编制主函数,任意输入一个大于4的偶数d,找出满足d=d1+d2的所有数对,其中要求d1与d2均为素数(通过调用prime来判断素数)。如偶数18可以分解为11+7以及13+5;而偶数80可以分解为:43+37、61+19、67+13、73+7。...