Cod sursa(job #2818667)

Utilizator luiz_felipeLuiz Felipe luiz_felipe Data 16 decembrie 2021 17:18:56
Problema Ciurul lui Eratosthenes Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>

using namespace std;
const int dim = 2000001 / 16 + 1;
ifstream f("ciur.in");
ofstream g("ciur.out");
int n, i, j, rez = 1;

inline void erase(unsigned char list[], int nr) {
    list[nr >> 3] &= ~(1 << (nr % 7));
}

inline bool check(const unsigned char list[], int nr) {
    return list[nr >> 3] & (1 << (nr % 7));
}

int main() {
    auto *sieve = new unsigned char[dim];
    for (i = 0; i < dim; ++i) sieve[i] = 0xFF;
    f >> n;
    for (i = 1; (((i * i) << 1) + (i << 1)) <= n; ++i)
        if (check(sieve, i)) {
            for (j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1) erase(sieve, j);
        }
    for (i = 1; (i << 1) + 1 <= n; ++i)
        if (check(sieve, i)) ++rez;
    g << rez;
    delete[] sieve;
}