Cod sursa(job #155616)

Utilizator nimicLeoveanu Mihaita Alexandru nimic Data 12 martie 2008 00:40:21
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <cstdio>
#define pow(x) (1u<<(((x)/2)&31))
#define clear(x) mask[(x)/64] |= pow(x)
#define isSet(x) !(mask[(x)/64] & pow(x))
#define count(x) (nrBits[(x)>>16] + nrBits[((x)<<16)>>16])

char nrBits[1<<16];
unsigned n, nr, total, mask[1<<16], sol[1000];

int main(){
    freopen("ciur.in", "r", stdin);
    freopen("ciur.out", "w", stdout);
    scanf("%d", &n);
    for (unsigned i=3; i*i<=n; i+=2)
        if (isSet(i))
           for (unsigned j=i*i; j<=n; j+=2*i)
               clear(j);
	for (unsigned i=1; i<(1<<16); i++)
		nrBits[i] = nrBits[i/2]+i%2;
	for (unsigned i=1; i<=64; i++)
	    clear(n+i);
	for (unsigned i=0; 64*i<n; i++)
		total += count(~mask[i]);
    printf("%d\n", total);
}