Cod sursa(job #2776687)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 20 septembrie 2021 18:28:55
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;

inline void Open(const string Name) {
    #ifndef ONLINE_JUDGE
        (void)!freopen((Name + ".in").c_str(), "r", stdin);
        (void)!freopen((Name + ".out").c_str(), "w", stdout);
    #endif
}

char p[6250001];

int GetPrimeNumbers(int N) {   
    for(int i = 1;((i * i) << 1) + (i << 1) <= N;i++) {
        if((p[i >> 3] & (1 << (i & 7))) == 0) {
            for (int j = ((i * i) << 1) + (i << 1);(j << 1) + 1 <= N;j += (i << 1) + 1) {
                p[j >> 3] |= (1 << (j & 7));
            }
        }
    }

    int nr = 1;
    for(int i = 1;2 * i + 1 <= N;i++)
        if((p[i >> 3] & (1 << (i & 7))) == 0) 
            nr++;
    return nr;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    Open("ciur");
    int N;

    cin >> N;
    cout << GetPrimeNumbers(N);
    
    return 0;
}