Cod sursa(job #3289253)

Utilizator Edi17roAnghel Eduard Edi17ro Data 26 martie 2025 11:48:06
Problema Ciurul lui Eratosthenes Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("ciur.in");
ofstream out("ciur.out");
const int NMAX =  1e6;
typedef unsigned char SET[(NMAX + 8) / 8];
typedef unsigned int victor[205];
int n;

inline void INSERT_IN_SET(SET &my_set, int n)
{
    my_set[n / 8] |= (1 << (n % 8));
}

inline bool QUERRY_SET(SET &my_set, int n)
{
    return (my_set[n / 8] & (1 << n % 8)) > 0;
}

void init_sieve(SET &my_set)
{
    INSERT_IN_SET(my_set, 0);
    INSERT_IN_SET(my_set, 1);

    for(int i = 2; i * i <= NMAX; ++i)
    {
        if(!QUERRY_SET(my_set, i))
        {
            for(int j = i * i; j <= NMAX; j += i)
            {
                INSERT_IN_SET(my_set, j);
            }
        }
    }
}

int main()
{
    in >> n;

    SET my_set = {0};
    init_sieve(my_set);
    int cnt = 0;

    for(int i = 1; i <= n; ++i)
    {
        if(!QUERRY_SET(my_set, i))
        {
            ++cnt;
        }
    }

    out << cnt;

    return 0;
}