链接:https://vjudge.net/problem/POJ-3126

Task

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
2
3
4
3
1033 8179
1373 8017
1033 1033

output:

1
2
3
6
7
0

Solution

每变动一位都要求是素数,首先需要一个素数判断算法。 判断n是否是素数,最粗暴的方法是在\([1,\sqrt{n}]\)的范围内枚举。但这样太慢了,需要一种快速算法。比如偶数一定不是素数。可以发现,大于等于5的素数只可能分布在6的倍数的左右两侧,即\(\{6k-1,6k+1\}\),其他部分\(\{6k,6k+2,6k+3,6k+4\}\)都一定不是素数,它们都能被2或3整除。所以对于2和3之外的素数,只需要在\([1,\sqrt{n}]\)的范围内每隔6枚举两个值就可。 用结构体表示一个4位数,元素num是当前数值,depth是替换的次数。用BFS遍历4位和每一位的10个可能值。每次替换一位时,用变量bit来表示被替换的那一位是多少,然后从0到9遍历可能的数值,如果替换后是素数,就将depth+1并加入队列。 注:用一个数组存储10的幂,避免重复计算。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
// based on the fact that:
// if a number n, n!=(6x+1) && n!=(6x+5), 
// then n cannot be a prime
// so we can only check (6x+1) and (6x+5)
// while searching for factor, only consider these prime proposals
// proof:
// n can only \in {6x, 6x+2, 6x+3, 6x+4}
// but they are all not prime
bool is_prime(int n){
    if(n==2 || n==3) return 1;
    if(n%6!=1 && n%6!=5) return 0;
    int n_sqrt=floor(sqrt((float)n));
    for(int i=5;i<=n_sqrt;i+=6)
        if(n%(i)==0 | n%(i+2)==0) return 0;
    return 1;
}
// return power of 10
int pow_10[5]={1,10,100,1000,10000};
struct node{
    int num;
    int depth;
};
int BFS(int first, int last){
    bool visit[9000];
    memset(visit,0,sizeof(visit));
    queue<node> q;
    while(!q.empty())
        q.pop();
    node node0={first,0};
    q.push(node0);
    while(!q.empty()){
        node node1=q.front();
        q.pop();
        if(node1.num==last)
            return node1.depth;
        for(int i=0; i<4; ++i){
            // which bit will be replaced
            int bit=node1.num%pow_10[4-i]/pow_10[3-i];
            for(int j=0; j<10; j+=1){
                // first bit cannot be 0
                if(i==0 && j==0)
                    continue;
                // itself
                if(bit==j)
                    continue;
                // after replace
                int num_new=node1.num+((j-bit)*pow_10[3-i]);
                if(num_new==last)
                    return node1.depth+1;
                if(visit[num_new-1000]==0 && is_prime(num_new))
                    q.push({num_new,node1.depth+1});
                visit[num_new-1000]=1;
            }
        }
    }
    return -1;
}
int main(){
    int N,first,last;
    char s_first[5],s_last[5];
    while(scanf("%d",&N)!=EOF){
        for(int i=0; i<N; ++i){
            scanf("%d %d",&first,&last);
            int result=BFS(first,last);
            printf("%d\n",result);
        }
    }
    return 0;
}