This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define LOGN 25
#define MAXN 200005
bool isPrime(ll x) {
if (x <= 1)
return false;
for (ll i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}
int main() {
fast
ll A, B;
cin >> A >> B;
// 0 1 2 3 4 5 6
vector<ll> cand_nodes{2, A-2, A, A+2, B-2, B, B+2};
vector<ll> nodes;
map<int,int> places;
int no=1;
for (auto u : cand_nodes) {
if (isPrime(u)) {
nodes.push_back(u);
places[u] = no++;
}
}
vector<ll> vis(10, false);
queue<vector<ll>> q;
q.push({A});
while (!q.empty()) {
vector<ll> path = q.front();
q.pop();
ll node = path.back();
if (vis[places[node]])
continue;
vis[places[node]] = true;
if (node == B) {
cout << path.size() << endl;
for (auto u : path)
cout << u << " ";
cout << endl;
return 0;
}
for (auto u : nodes) {
if (u != node && isPrime(abs(u - node))) {
path.push_back(u);
q.push(path);
path.pop_back();
}
}
}
cout << -1 << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |