POJ 3126 Prime Path
2015-07-29 11:33
309 查看
C - Prime Path
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
3126
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.
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
Sample Output
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
3126
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.
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
3 1033 8179 1373 8017 1033 1033
Sample Output
6 7 0
#include <iostream> #include<cstring> using namespace std; int p[10100]={0},q[10100][2],h[10100],a[5],b[5]; int trans(){ return b[0]+b[1]*10+b[2]*100+b[3]*1000; } int main() { int i,j,n,s,tt; p[1]=1; for (i=2;i<=5000;i++){ if (!p[i]){ j=i; while (j+i<10000){ j=j+i; p[j]=1; } } } //筛法求素数
cin>>n; int f,t,flag,x; while (n--){ cin>>s>>tt; memset(h,0,sizeof(h)); h[s]=1; q[1][0]=s;q[1][1]=0; f=0;t=1;flag=0; if (s!=tt) { while (f<t){ f++; x=q[f][0]; a[0]=x%10;x=x/10; a[1]=x%10;x=x/10; a[2]=x%10;x=x/10; a[3]=x%10;x=x/10; for (i=0;i<4;i++){ b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[3]; for (j=0;j<=9;j++){ if ((j==0)&&(i==3)) continue; if (b[i]==j) continue; b[i]=j; x=trans(); if (!h[x] && !p[x]){ h[x]=1; t++; q[t][0]=x;q[t][1]=q[f][1]+1; if (x==tt) {flag=1;break;} } } if (flag) break; } if (flag) break; } } if (s==tt) cout<<0<<endl; else { if (flag) cout<<q[t][1]<<endl; else cout<<"Impossible"<<endl; } } return 0; }
相关文章推荐
- 可视化日历程序
- php抓取网页内容
- java集合中List与set的区别
- Syscall param open(filename) points to unaddressable byte(s)
- tcmalloc资料
- qt-creator astyle Peizhi
- Python通过90行代码搭建一个音乐搜索工具
- linux netstat 笔记
- IIS发布ASP网页错误
- 关于oracle用户密码过期的解决方法
- ExpanListView LongClick
- Android之TelephonyManager类的方法详解
- qt对象销毁过程delete
- 查找算法
- 一个使用Directx渲染的Havok程序
- tomcat部署javaweb程序的三种方式
- 让删除的数据彻底不可恢复
- zz 通过INFORMATION_SCHEMA.INNODB_TRX、INNODB_LOCKS、INNODB_LOCK_WAITS 三个表获取事务与锁的信息
- Android4.0系统接收不到广播的问题解析
- 为xampp 安装pear db (database) 模块