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
|
#include <cstdio> #include <queue> #include <iostream> #include <cstring> #define inf 10000 const int N = 1e4 + 66; using namespace std;
int T, cnt, a[66]; int vis[N], pri[N], v[N];
inline void pre() { memset(vis, 1, sizeof vis); vis[1] = 0; int i, j; for (i = 2; i <= inf; ++ i) { if (vis[i]) pri[++ cnt] = i; for (j = 1; j <= cnt && i * pri[j] <= inf; ++ j) { vis[i * pri[j]] = 0; if (! (i % pri[j])) break; } } return; }
inline int youngore() { scanf ("%d", &T); pre(); while (T --> 0) { int x, y, i, j; scanf ("%d%d", &x, &y); memset(v, 0, sizeof v); queue<pair<int,int> >q; q.push(make_pair(x,0)); v[x] = 1; while (! q.empty()) { pair<int, int> nows = q.front(); q.pop(); if (nows.first == y) { cout << nows.second << '\n'; break; } int k = nows.first, cnt = 0; while (k) { a[++ cnt] = k % 10; k /= 10; } for (k = cnt; k >= 1; -- k) { for (i = ((k == cnt) ? 1 : 0); i <= 9; ++ i) { if (i == a[k]) continue; int res = 0; for (j = cnt; j >= 1; -- j) res = res * 10 + ((j == k) ? i : a[j]); if (vis[res] && !v[res]) q.push(make_pair(res, nows.second + 1)), v[res] = 1; } } } } return 0; }
int search_ex = youngore();
signed main() {;}
|