Initial commit
This commit is contained in:
45
2022/republic/d0/problem04/maxim_solution.cpp
Normal file
45
2022/republic/d0/problem04/maxim_solution.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
typedef long long ll;
|
||||
#define f first
|
||||
#define s second
|
||||
#define pb push_back
|
||||
#define mp make_pair
|
||||
#define pi pair<ll, ll>
|
||||
#define sz(x) (int)((x).size())
|
||||
#define all(a) (a).begin(), (a).end()
|
||||
|
||||
|
||||
const ll mod = 1e9+7;
|
||||
ll n, k, m, mi, ma;
|
||||
|
||||
void solve(){
|
||||
|
||||
cin >> n >> m;
|
||||
vector<ll> a(m + 5, 0), ans;
|
||||
bool u = 1;
|
||||
for(int i = 2; i <= m; i++){
|
||||
if(a[i] != 0) continue;
|
||||
if(n <= i && i <= m) {
|
||||
cout << i << " ";
|
||||
u = 0;
|
||||
}
|
||||
for(int j = i; j<=m; j+=i) a[j] = i;
|
||||
}
|
||||
if(u) cout << "Absent";
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
//init();
|
||||
|
||||
int t=1;
|
||||
//cin >> t;
|
||||
while(t--) solve();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
2022/republic/d0/problem04/problem04 (ru).pdf
Normal file
BIN
2022/republic/d0/problem04/problem04 (ru).pdf
Normal file
Binary file not shown.
39
2022/republic/d0/problem04/problem04.cpp
Normal file
39
2022/republic/d0/problem04/problem04.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
int start, end;
|
||||
|
||||
cin >> start >> end;
|
||||
|
||||
int notPrimes[end + 5] = {0};
|
||||
bool foundPrime = false;
|
||||
|
||||
for (int i = 2; i <= end; i++) {
|
||||
if (notPrimes[i] != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (start <= i && i <= end) {
|
||||
cout << i << " ";
|
||||
foundPrime = true;
|
||||
}
|
||||
|
||||
for (int j = i; j <= end; j += i) {
|
||||
notPrimes[j] = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundPrime) {
|
||||
cout << "Absent" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
100
2022/republic/d0/problem04/try1.cpp
Normal file
100
2022/republic/d0/problem04/try1.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
int start, end;
|
||||
|
||||
cin >> start;
|
||||
cin >> end;
|
||||
// const int start = 1;
|
||||
// const int end = 1000000;
|
||||
|
||||
|
||||
|
||||
list<int> primes;
|
||||
|
||||
primes.push_back(1);
|
||||
primes.push_back(2);
|
||||
primes.push_back(3);
|
||||
primes.push_back(5);
|
||||
|
||||
// Fill all numbers, excluding most obvious non-primes numbers
|
||||
for (int i = 6; i <= end; i++) {
|
||||
if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {
|
||||
continue;
|
||||
}
|
||||
primes.push_back(i);
|
||||
}
|
||||
|
||||
auto it = primes.begin();
|
||||
advance(it, 4);
|
||||
|
||||
int stop = sqrt(end);
|
||||
for (it; *it <= stop; it++) {
|
||||
/*
|
||||
Go through all numbers in the list that are lower than sqrt(end)
|
||||
The list is modified in the loop, allowing to skip processing of some numbers
|
||||
*/
|
||||
int factor = *it;
|
||||
|
||||
auto it2 = it;
|
||||
it2++;
|
||||
|
||||
auto stop2 = primes.end();
|
||||
for (it2; it2 != stop2; it2++) {
|
||||
/*
|
||||
Go through all numbers that are greater than the *it,
|
||||
removing them if they can be divided by *it
|
||||
*/
|
||||
|
||||
if (*it2 % factor == 0) {
|
||||
auto tmp = next(it2);
|
||||
|
||||
primes.erase(it2);
|
||||
|
||||
it2 = prev(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A dumb way to handle end < 5
|
||||
if (end < primes.back()) {
|
||||
auto trueEnd = primes.begin();
|
||||
auto stop = primes.end();
|
||||
while (trueEnd != stop && *trueEnd <= end) {
|
||||
trueEnd++;
|
||||
}
|
||||
primes.erase(trueEnd, stop);
|
||||
}
|
||||
|
||||
|
||||
auto startIt = primes.end();
|
||||
auto primesEnd = primes.end();
|
||||
|
||||
it = primes.begin();
|
||||
for (it; it != primesEnd; it++) {
|
||||
if (*it >= start) {
|
||||
startIt = it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (start == end && *startIt != start) {
|
||||
// Range contains only one number and it's not prime
|
||||
cout << "Absent" << endl;
|
||||
} else if (it == primesEnd) {
|
||||
cout << "Absent" << endl;
|
||||
} else {
|
||||
it = startIt;
|
||||
for (it; it != primesEnd; it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user