Cod sursa(job #2442004)

Utilizator Ilie_MityIlie Dumitru Ilie_Mity Data 22 iulie 2019 13:34:12
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include<cstdio>

using namespace std;

char ciur[125001];

int main()
{
    FILE *f, *g;
    int n, i, j, rez = 1;

    f=fopen("ciur.in", "r");

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

    fclose(f);

    /*for(i = 2; i <= n; ++i)
        if(!ciur[i])
        {
            ++rez;
            for(j = i * i; j <= n; j += i)
                ciur[j] = true;
        }*/
    for(i = 1; ((i * i + i) << 2) + 1 <= n; ++i)
        if(!(ciur[i >> 3] & (1 << (i & 7))))
            for(j = (i * i + i) << 1; (j << 1) + 1 <= n; j += (i << 1) + 1)
                ciur[j >> 3] |= 1 << (j & 7);

    for(i = 1; (i << 1) + 1 <= n; ++i)
        if(!(ciur[i >> 3] & (1 << (i & 7))))
            ++rez;

    g=fopen("ciur.out", "w");

    fprintf(g, "%i", rez);

    fclose(g);

    return 0;
}