Cod sursa(job #2151482)

Utilizator 2016Teo@Balan 2016 Data 4 martie 2018 15:49:29
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>
using namespace std;
ifstream g("ciur.in");
ofstream gg("ciur.out");
int SieveOfAtkin(int limit) {
  int c=0;
    if (limit > 2)  c++;
    if (limit > 3)  c++;
    bool ciur[limit];
    for (int i=0; i<limit; i++)
        ciur[i] = false;
    for (int x = 1; x*x < limit; x++) {
        for (int y = 1; y*y < limit; y++) {
            int n = (4*x*x)+(y*y);
            if (n <= limit && (n % 12 == 1 || n % 12 == 5))
                ciur[n] ^= true;
            n = (3*x*x)+(y*y);
            if (n <= limit && n % 12 == 7)
                ciur[n] ^= true;

            n = (3*x*x)-(y*y);
            if (x > y && n <= limit && n % 12 == 11)
                ciur[n] ^= true;
        }
    }
    for (int r = 5; r*r < limit; r++) {
        if (ciur[r]) {
            for (int i = r*r; i < limit; i += r*r)
                ciur[i] = false;
        }
    }
    for (int a = 5; a < limit; a++)
        if (ciur[a])
            c++;
      return c;
}
int main(void) {
    int limit ;
    g>>limit;
    gg<<SieveOfAtkin(limit);
    return 0;
}