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;
long long M;
long long qexp (long long b, long long e) { //quick exponentiation (see below)
if (e == 0) return 1;
if (e == 1) return b%M;
long long h = qexp(b, e/2);
h *= h;
h %= M;
if (e%2 == 0) return h;
return (h*b)%M;
}
bool isPrime(long long N) {
if (N <= 1) return 0;
if (N <= 3) return 1;
if (N == 4) return 0;
long long s = (N-1)&(1-N), d = (N-1)/s;
M = N;
for (long long i = 0, K = 30; i < K; ++i) {
long long witness = rand()%(N-4) + 2; //generate random witness between [2, N-2]
long long a = qexp(witness, d);
if (a == 1 || a == N-1) continue;
bool pass = 1;
for (long long k = 1; k < s && pass; k<<=1) {
a*=a;
a%=N;
if (a == N-1) pass = false;
}
if (pass) return 0; /* not a prime due to witness */
}
return 1;
}
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 << '\n';
}
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... |