Cod sursa(job #3213113)

Utilizator AlexInfoIordachioaiei Alex AlexInfo Data 12 martie 2024 15:56:53
Problema Ciurul lui Eratosthenes Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("ciur.in");
ofstream out("ciur.out");

#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second

const int NMAX = 1e5 + 30;
const int INF = 0x3f3f3f3f;

int n, ans;
bool ciur[NMAX];

void read()
{
    in >> n;
}

void eratostene(int n)
{
    // ciur[i] = 1 (non prim) sau 0 (prim)
    ciur[1] = ciur[0] = 1;
    ciur[2] = 0;
    // multiplii unui nr prim nu sunt primi
    for (int i = 2; i*i <= n; i++)
        if (!ciur[i])
        {
            for (int j = 2; j*i <= n; j++)
                ciur[i*j] = 1;
        }
    

}

void solve()
{
    eratostene(n);
    for (int i=2; i<=n; i++) if (!ciur[i]) ans++;
    out << ans;
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);

    read();
    solve();

    return 0;
}