Cod sursa(job #2765444)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 26 iulie 2021 21:19:31
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 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
}

int prime[31250];
int N;


bool ifnotPrime(int x) {
    return (prime[x / 64] & (1 << ((x >> 1) & 31)));
}

void makeComposite(int x) {
    prime[x / 64] |= (1 << ((x >> 1) & 31));
}

void bitwiseSieve() {
    for(long long i = 3;i * i <= N;i += 2)
        if(!ifnotPrime((int)i)) {
            for(long long j = i * i, k = i << 1LL;j <= N;j += k)
                makeComposite((int)j);
        }

    int ans = 1;
    for(int i = 3;i <= N;i += 2)
        if(!ifnotPrime(i)) ans++;

    cout << ans;
}

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

    Open("ciur");

    cin >> N;

    if(N == 1) {
        cout << 0;
        return 0;
    }

    bitwiseSieve();
}