# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
823871 | Sandarach151 | Lutrija (COCI19_lutrija) | C++17 | 1 ms | 320 KiB |
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;
vector<long long> vect;
cin >> a >> b;
bool swapped = false;
if(a==2){
swap(a, b);
swapped = true;
}
long long tempa = a;
bool res1 = true;
while(!isPrime(abs(b-a))){
a+=2;
if(!isPrime(a)){
res1 = false;
break;
}
}
a = tempa;
bool res2 = true;
while(!isPrime(abs(b-a))){
a-=2;
if(!isPrime(a)){
res2 = false;
break;
}
}
if(!res1 && !res2){
cout << -1 << '\n';
}
else{
if(res1){
a = tempa;
vect.push_back(a);
while(!isPrime(abs(b-a))){
a+=2;
vect.push_back(a);
}
vect.push_back(b);
}
else{
a = tempa;
vect.push_back(a);
while(!isPrime(abs(b-a))){
a-=2;
vect.push_back(a);
}
vect.push_back(b);
}
if(!swapped){
cout << vect.size() << '\n';
for(long long i=0; i<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;
}
Compilation message (stderr)
# | 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... |