#include<bits/stdc++.h>
using namespace std;
bool isPrime(long long n){
if(n < 2)return 0;
if(n == 2)return 1;
if(n % 2 == 0)return 0;
for(long long i = 3;i * i <= n;i += 2){
if(n % i == 0)return 0;
}
return 1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
long long a, b;
cin >> a >> b;
map<long long, vector<long long>>G;
for(int i = -2;i <= 2;i += 2){
if(i && isPrime(a + i)){
G[a].push_back(a + i);
G[a + i].push_back(a);
}
for(int j = -2;j <= 2;j += 2){
if(isPrime(a + i) && isPrime(b + j) && isPrime(abs(a + i - b - j))){
G[a + i].push_back(b + j);
G[b + j].push_back(a + i);
}
if(isPrime(a + i) && isPrime(abs(a + i - 2))){
G[2].push_back(a + i);
G[a + i].push_back(2);
}
if(isPrime(b + j) && isPrime(abs(b + j - 2))){
G[2].push_back(b + j);
G[b + j].push_back(2);
}
}
}
for(int j = -2;j <= 2;j += 2){
if(j && isPrime(b + j)){
G[b].push_back(b + j);
G[b + j].push_back(b);
}
}
map<long long, int>dist;
dist[a] = 0;
map<long long, long long>par;
par[a] = a;
queue<int>q;
q.push(a);
while(!q.empty()){
long long cur = q.front();
q.pop();
for(long long &v : G[cur]){
if(!dist.count(v) || dist[v] > dist[cur] + 1){
dist[v] = dist[cur] + 1;
par[v] = cur;
q.push(v);
}
}
}
if(!dist.count(b)){
cout << -1;
}
else{
vector<long long>path;
long long node = b;
while(node != a){
path.push_back(node);
node = par[node];
}
path.push_back(a);
reverse(path.begin(), path.end());
cout << path.size() << '\n';
for(long long &v : path)cout << v << " ";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
960 ms |
432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
943 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1054 ms |
424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
792 ms |
428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |