Cod sursa(job #2848619)

Utilizator om6gaLungu Adrian om6ga Data 12 februarie 2022 22:31:30
Problema Ciurul lui Eratosthenes Scor 100
Compilator c-32 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <stdio.h>
//#include <stdlib.h>
//#include <unistd.h>
//#include <time.h>
 
#pragma GCC optimize("Ofast")
 
int i, j, N, nr = 0, double_i, half_i;
unsigned int bitmap[31250];
unsigned int r[32];

//struct timespec start, stop;

int main()
{ 
	freopen( "ciur.in", "r", stdin );
	freopen( "ciur.out", "w", stdout);  
    scanf("%d",&N);

    //clock_gettime( CLOCK_REALTIME, &start);
    r[0] = 1;
    for (i = 1; i < 32; i++) {
    	r[i] = r[i-1]*2;
	}

    for (i = 3; (j = i*i) <= N; i += 2) {
        if (!(bitmap[i>>6] & r[(i>>1) & 0x1f])) {
        	bitmap[j>>6] |= r[(j>>1) & 0x1f];
        	nr++;
        	j += (double_i = (i<<1));
            for (; j <= N; j += double_i) {
                if (!(bitmap[j>>6] & r[(j>>1) & 0x1f])) {
                	bitmap[j>>6] |= r[(j>>1) & 0x1f];
                	nr++;
				}
            }
		}
    }
    //clock_gettime( CLOCK_REALTIME, &stop);
    //printf( "%d seconds %d nanoseconds\n", stop.tv_sec - start.tv_sec, stop.tv_nsec - start.tv_nsec);
 
    printf("%d\n", (N+1)/2 - nr);
    return 0;
}