Cod sursa(job #2422832)

Utilizator melutMelut Zaid melut Data 20 mai 2019 01:35:59
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <string>
#include <bitset>

using namespace std;


typedef unsigned long long ULL;


string const inFile = "ciur.in";
string const outFile = "ciur.out";

const unsigned MAX_N = 2 * 1000 * 1000 + 1;


ifstream Read(inFile);
ofstream Write(outFile);


void Sieve(unsigned const length) {
    bitset<MAX_N> prime;
    unsigned _count = 1;
    unsigned i, j;
    unsigned increment;

    for (i = 3; i <= length; i += 2) {
        if (!prime[i]) {
            ++_count;

            if (ULL(i) * ULL(i) > length) {
                continue;
            }

            increment = i << 1;

            for (j = i * i; j <= length; j += increment) {
                prime.set(j);
            }
        }
    }

    Write << _count;
}


int main() {
    unsigned n;
    Read >> n;

    Sieve(n);

    return 0;
}