#include<iostream>
#include<vector>
#include<queue>
#include<tuple>
#include<map>
#include<set>
using namespace std;
#define int long long
#ifdef lioraju
#define ndbg(x)
#else
#define ndbg(x) x
#endif
typedef tuple<int, int> p2i;
signed main()
{
ndbg( ios::sync_with_stdio(0); cin.tie(0); );
auto checkPrime = [](int n)
{
if (n<0) n = -n;
if (n<2) return 0;
for (int i=2;i*i<=n;i++)
if (n%i==0) return 0;
return 1;
};
map<int, int> vfr;
int st, ed; cin>>st>>ed;
if (st>ed) swap(st, ed);
queue<p2i> qu; qu.push(p2i{st, -1});
auto qu_push = [&](int np, int p)
{
if (np<2 || vfr.count(np) || !checkPrime(np))
return;
vfr[np]=-1, qu.push(p2i{np, p});
};
while (qu.size())
{
int p, fr; tie(p, fr) = qu.front(); qu.pop();
vfr[p] = fr;
if (p==ed)
{
int now = p;
vector<int> v;
while (now!=-1)
{
v.push_back(now);
now = vfr[now];
}
cout<<v.size()<<'\n';
for (int x: v) cout<<x<<' ';
cout<<'\n';
return 0;
}
if (!vfr.count(2) && checkPrime(p-2))
vfr[2]=-1, qu.push(p2i{2, p});
if (p!=2)
{
for (int vi=-2;vi<=2;vi+=4)
{
int np = p+vi;
qu_push(np, p);
}
}
else
{
for (int vi=-10;vi<=10;vi+=2)
{
if (checkPrime(st+vi-p)) qu_push(st+vi, p);
if (checkPrime(ed+vi-p)) qu_push(ed+vi, p);
}
}
}
cout<<"-1\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
300 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
753 ms |
392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
728 ms |
380 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Incorrect |
77 ms |
376 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
794 ms |
392 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Incorrect |
70 ms |
376 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
565 ms |
352 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Incorrect |
28 ms |
376 KB |
Output isn't correct |