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;
bool isPrime(long long x) {
if (x < 2) return false; //All primes are above 2
else if (x == 2) return true; //If its 2, its a prime
else if (x % 2 == 0) return false; //If its even (other than 2), its not a prime -> used for speedup
for (int i = 3; i <= x/i; i+=2) { //Trial divide all odd numbers > 2 up till the square root of x
if (x % i == 0) return false; //If a factor is found, x is not prime
}
return true; //If no factors are found, x is a prime
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long a, b;
cin >> a >> b;
bool res = true;
vector<long long> vect;
bool swapped = false;
if(a%2==1 && b%2==1){
if(a>b){
swap(a, b);
swapped = true;
}
for(int i=a; i<=b; i+=2){
if(!isPrime(i)){
res = false;
break;
}
vect.push_back(i);
}
}
else{
if(a==2){
swap(a, b);
swapped = true;
}
if(isPrime(a) && isPrime(a+2)){
vect.push_back(a);
vect.push_back(a+2);
vect.push_back(2);
}
else if(isPrime(a) && isPrime(a-2)){
vect.push_back(a);
vect.push_back(2);
}
else{
res = false;
}
}
if(!res){
cout << -1;
}
else{
if(!swapped){
cout << vect.size() << '\n';
for(long long i=0LL; i< (long long) vect.size(); i++){
cout << vect[i] << ' ';
}
}
else{
cout << vect.size() << '\n';
for(long long i=vect.size()-1; i>=0; i--){
cout << vect[i] << ' ';
}
}
}
return 0;
}
# | 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... |