Prime Path(POJ-3126)

    科技2025-10-18  12

    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

    分析

    先把素数表打好然后个,十,百,千逐位跑bfs() 注意一下千位是要从1开始的,其他的就直接搜即可

    Code

    #include<iostream> #include<algorithm> #include<math.h> #include<string> #include<vector> #include<queue> #include<string.h> using namespace std; typedef long long ll; const int maxn = 1e5+6; int vis[maxn]; bool isPrime_2_b[maxn]; bool isPrime_a_b[maxn]; struct node { int pos; int step; node(int pos , int step) { this->pos = pos; this->step= step; } }; void segment_sieve(ll a, ll b)//埃氏筛区间[a,b)素数 { for(int i = 0 ; (ll)i*i < b ; i++) { isPrime_2_b[i] = true; } for(int i = 0 ; i < b - a ; i++) { isPrime_a_b[i] = true; } for(int i = 2 ; (ll)i*i < b ; i++) { if(isPrime_2_b[i]) { for(int j = 2 * i ; (ll)j * j < b ; j += i) { isPrime_2_b[j] = false; } for(ll j = max(2LL , (a+i-1)/i)*i ; j < b ; j += i) { isPrime_a_b[j-a] = false; } } } } void bfs(int l,int r) { memset(vis,0,sizeof(vis)); queue<node>q; q.push(node(l,0)); vis[l] = 1; while(!q.empty()) { node now = q.front(); q.pop(); if(now.pos == r) { printf("%d\n",now.step); break ; } for(int i = 1 ; i <= 4 ; i++) { if(i == 1)//跑个位 { for(int j = 0 ; j <= 9 ; j++) { node next = now; next.pos = now.pos/10*10 + j; if(isPrime_a_b[next.pos - 1000] && vis[next.pos] == 0 && next.pos != now.pos) { vis[next.pos] = 1; next.step++; q.push(next); } } } else if(i == 2)//跑十位 { for(int j = 0 ; j <= 9 ; j++) { node next = now; next.pos = now.pos/100*100 + j*10 + now.pos%10; if(isPrime_a_b[next.pos - 1000] && vis[next.pos] == 0 && next.pos != now.pos) { vis[next.pos] = 1; next.step++ ; q.push(next); } } } else if(i == 3)//跑百位 { for(int j = 0 ; j <= 9 ; j++) { node next = now; next.pos = now.pos/1000*1000 + j*100 + now.pos%100; if(isPrime_a_b[next.pos - 1000] && vis[next.pos] == 0 && next.pos != now.pos) { vis[next.pos] = 1; next.step++; q.push(next); } } } else if(i == 4)//跑千位 { for(int j = 1 ; j <= 9 ; j++)//千位要从1开始 { node next = now; next.pos = j*1000 + now.pos%1000; // cout<<"next.pos = "<<next.pos<<endl; if(isPrime_a_b[next.pos - 1000] && vis[next.pos] == 0 && next.pos != now.pos) { vis[next.pos] = 1; next.step ++; q.push(next); } } } } } } int main() { segment_sieve(1000,10000); int t; scanf("%d",&t); while(t--) { int l,r; scanf("%d %d",&l,&r); bfs(l,r); } return 0; }
    Processed: 0.010, SQL: 8