Cod sursa(job #1870211)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 6 februarie 2017 14:51:49
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <stdio.h>
#include <bitset>

using namespace std;

#define ll long long unsigned
#define pb push_back
#define mp make_pair

bitset <2000005> ciur;

int sieve(int n){
    int i,j;
    int ans = 1;
    for(i = 4;i <= n;i += 2){
        ciur[i] = 1;
    }
    for(i = 3;i*i <= n;i += 2){
        if(ciur[i] == 0){
            ans++;
            for(j = i*i;j <= n;j += 2*i){
                ciur[j] = 1;
            }
        }
    }
    for(;i <= n;i += 2){
        if(ciur[i] == 0){
            ans++;
        }
    }
    return ans;
}

int main(){
    int n;
    freopen("ciur.in", "r", stdin);
    freopen("ciur.out", "w", stdout);
    scanf("%d",&n);
    printf("%d",sieve(n));
    return 0;
}