#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;
}
ll x = closest2(a);
ll y = closest2(b);
if (x==-1||y==-1){
bool works = true;
for (ll i = a; (i!=b&&abs(a-b)<62); i+=2*((b-a)/abs(b-a))){
if (!isPrime(i)){
works=false;
}
}
if (works){
vector<ll> ans;
for (ll i = a; (i!=b&&abs(a-b)<62); 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;
}
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 |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Incorrect |
4 ms |
256 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Incorrect |
4 ms |
256 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Incorrect |
4 ms |
256 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
512 KB |
Output is correct |
2 |
Incorrect |
182 ms |
256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
236 ms |
256 KB |
Output is correct |
2 |
Incorrect |
154 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
384 KB |
Output is correct |
2 |
Incorrect |
178 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
195 ms |
384 KB |
Output is correct |
2 |
Incorrect |
145 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |