#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool isPrime(ll p){
//cout<<p<<endl;
if (p<2){
return false;
}
if (p==2){
return true;
}
if (p%2==0){
return false;
}
for (ll i = 3; i*i<=p; i+=2){
if (p%i==0){
return false;
}
}
return true;
}
ll closest2(ll x){
ll mindist = 1e9;
ll minval = -1;
for (ll i = x; i<=x+62; i+=2){
if (!isPrime(i)){
break;
}
if (isPrime(abs(i-2))){
mindist=abs(i-x);
minval=i;
break;
}
}
for (ll i = x-2; i>=max(0LL,x-62); i-=2){
if (!isPrime(i)){
break;
}
if (isPrime(abs(i-2))){
if (abs(i-x)<mindist){
mindist=abs(i-x);
minval=i;
}
break;
}
}
return minval;
}
int main(){
ll a,b;
cin>>a>>b;
bool rev = false;
//is one 2
if (b==2){
rev = true;
swap(a,b);
}
if (a==2){
ll c = closest2(b);
if (c==-1){
cout<<-1<<endl;
return 0;
}
vector<ll> ans;
ans.push_back(2);
while (c!=b){
ans.push_back(c);
c+=2*((b-c)/abs(b-c));
}
ans.push_back(c);
if (rev){
reverse(ans.begin(),ans.end());
}
assert(ans.size()<=30);
cout<<ans.size()<<'\n';
for (ll i : ans){
cout<<i<<" ";
}cout<<'\n';
return 0;
}
bool works = true;
for (ll i = a; i!=b; i+=2*((b-a)/abs(b-a))){
if (!isPrime(i)||abs(i-b)>62){
works=false;
break;
}
}
if (works){
vector<ll> ans;
for (ll i = a; i!=b; i+=2*((b-a)/abs(b-a))){
ans.push_back(i);
}ans.push_back(b);
assert(ans.size()<=30);
cout<<ans.size()<<'\n';
for (ll i : ans){
cout<<i<<" ";
}cout<<'\n';
return 0;
}
ll x = closest2(a);
ll y = closest2(b);
if (x==-1||y==-1){
cout<<-1<<'\n';
return 0;
}
vector<ll> ans;
while (a!=x){
ans.push_back(a);
a+=2*((x-a)/abs(x-a));
}
ans.push_back(a);
ans.push_back(2);
while (y!=b){
ans.push_back(y);
y+=2*((b-y)/abs(b-y));
}
ans.push_back(y);
assert(ans.size()<=30);
cout<<ans.size()<<'\n';
for (ll i : ans){
cout<<i<<" ";
}cout<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
279 ms |
356 KB |
Output is correct |
2 |
Correct |
193 ms |
356 KB |
Output is correct |
3 |
Correct |
52 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
275 ms |
256 KB |
Output is correct |
2 |
Correct |
165 ms |
256 KB |
Output is correct |
3 |
Correct |
39 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
319 ms |
256 KB |
Output is correct |
2 |
Correct |
210 ms |
364 KB |
Output is correct |
3 |
Correct |
37 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
504 KB |
Output is correct |
2 |
Correct |
143 ms |
384 KB |
Output is correct |
3 |
Correct |
16 ms |
256 KB |
Output is correct |