Cod sursa(job #1700421)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 10 mai 2016 15:00:04
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>

using namespace std;

int n , i , ans;
vector < unsigned char > low;
vector < int > p;

int main()
{
    freopen("ciur.in","r",stdin);
    freopen("ciur.out","w",stdout);

    scanf("%d", &n);
    low = vector < unsigned char > (n + 1 , 0);

    p.push_back(0);

    for (i = 2; i <= n; ++i)
    {
        if (low[i] == 0)
        {
            ans++;
            if (i <= 1500) p.push_back(i);
        }

        for (int j = 1; j < (int)p.size(); ++j)
        {
            int it = p[j];
            if ((it > p[low[i]] && low[i] != 0) || it * i > n) break;
            low[it*i] = j;
        }
    }

    printf("%d\n", ans);

    return 0;
}