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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <iostream> #include <string.h> #include <vector> #include <queue> #include <cmath> using namespace std;
const int N = 10000; bool a[N + 1]; vector<int> prime; void get_prime() { memset(a, true, sizeof(a)); for (int i = 2; i <= N; i++) { if (a[i]) prime.push_back(i); for (int j = 0; j < prime.size() && i * prime[j] <= N; j++) { a[i * prime[j]] = false; if (i % prime[j]==0) break; } } }
struct Node{ int number; int money; };
queue<Node>qu; int start,ended; int vis[N] = {0}; void bfs() { int bit,i,j,k,temp; Node first_node; Node temp_node; Node now_node;
first_node.number = start; first_node.money = 0; qu = queue<Node>(); qu.push(first_node); memset(vis,0,sizeof(vis)); while(!qu.empty()) { now_node = qu.front(); if (now_node.number == ended) { cout << now_node.money << endl; return; } temp = now_node.number; for (i = 0; i < 3; i++) { bit = temp%10; for (j = bit + 1, k = 1; j < 10; j++, k++) { if (a[now_node.number + k * (int) pow(10, i)] && !vis[now_node.number + k * (int) pow(10, i)]) { temp_node.number = now_node.number + (int) k * pow(10, i); temp_node.money = now_node.money + 1; vis[temp_node.number] = 1; qu.push(temp_node); } } for (j = bit - 1, k = 1; j >= 0; k++, j--) { if (a[now_node.number - k * (int) pow(10, i)] && !vis[now_node.number - k * (int) pow(10, i)]) { temp_node.number = now_node.number - (int) k * pow(10, i); temp_node.money = now_node.money + 1; vis[temp_node.number] = 1; qu.push(temp_node); } } temp/=10; } bit = temp%10; for (j = bit + 1, k = 1; j < 10; j++, k++){ if (a[now_node.number + 1000 * k] && !vis[now_node.number + 1000 * k]) { temp_node.number = now_node.number+1000*k; temp_node.money = now_node.money+1; vis[temp_node.number] = 1; qu.push(temp_node); } } for (j = bit - 1, k = 1; j > 0; j--, k++) { if (a[now_node.number - 1000 * k] && !vis[now_node.number - 1000 * k]) { temp_node.number = now_node.number - 1000 * k; temp_node.money = now_node.money + 1; vis[temp_node.number] = 1; qu.push(temp_node); } } qu.pop(); } }
int main() { int k,i;
get_prime(); cin>>k; for(i=0;i<k;i++){ cin>>start>>ended; bfs(); } return 0; }
|