Cod sursa(job #2048504)

Utilizator Iorgus08Iorgus Serghei Cicala Iorgus08 Data 26 octombrie 2017 09:12:08
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>

#define MAXSIZE 100000000/2/8+1

using namespace std;

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

char p[MAXSIZE];

int ciur(int n)
{
    int i, j, nr = 1;
    for (i = 1; ((i * i) << 1) + (i << 1) <= n; i += 1)
    {
        if ((p[i >> 3] & (1 << (i & 7))) == 0)
        {
            for (j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1)
            {
                p[j >> 3] |= (1 << (j & 7));
            }
        }
    }
    for (i = 1; 2 * i + 1 <= n; ++i)
        if ((p[i >> 3] & (1 << (i & 7))) == 0)
            nr++;
    return nr;
}
int main()
{
    long long x;
    in>>x;
    out<<ciur(x);
    return 0;
}