Cod sursa(job #1013670)

Utilizator mucenic_b101Bogdan Mucenic mucenic_b101 Data 21 octombrie 2013 15:41:29
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include<stdio.h>
#include<iostream>

using namespace std;

#define MAXN 250001

unsigned char ciur[MAXN];

int main () {

    FILE *f,*g;
    f=fopen("ciur.in","r");
    g=fopen("ciur.out","w");

    int n, nr;

    fscanf(f,"%d",&n);

    ciur[0] |= 3;
    for( int i = 2; i*i <= n; ++i)
        for( int j= i * i ; j <= n; j += i )
        {
            ciur[j/8] |= (1<<(j%8));
        }
    //ciur[0]=ciur[1]=1;
    nr=0;
   // cout<< (int)ciur[0]<<" "<<(int)ciur[1]<<" ";
    for( int i=0; i< n/8; ++i)
        for( int j = 0; j <= 7; ++j)
            if(!(ciur[i] & (1<<j)))
                nr++;
    for( int j = 0 ; j <= n%8; ++j)
        if(!(ciur[n/8] & (1<<j)))
                nr++;

    fprintf(g,"%d\n",nr);

    fclose(f);
    fclose(g);

    return 0;
}