Cod sursa(job #1808636)

Utilizator medicinedoctoralexandru medicinedoctor Data 17 noiembrie 2016 22:03:58
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

vector <bool> a;
int n,c=0;

void ciur()
{
    int q,k;
    for (int x=1; x*x<=n; x++)
    {
        for (int y=1; y*y<=n; y++)
        {
            q=4*x*x+y*y;
            if (q<=n && (q % 12 == 1 || q % 12 == 5)) a[q]=(!a[q]);
            q-=x*x;
            if (q<=n && q % 12 == 7) a[q]=(!a[q]);
            q-=2*y*y;
            if (q<=n && x>y && q % 12 == 11) a[q]=(!a[q]);
        }
    }
    for (int i=5; i*i<=n; i++)
    {
        if (a[i])
        {
            k=i*i; q=k;
            while (q<=n)
            {
                a[q]=false;
                q+=k;
            }
        }
    }
    a[2]=true;
    a[3]=true;
}

main()
{
    cin >> n;
    a.resize(n+1);
    ciur();
    for (int i=2; i<=n; i++)
    {
        if (a[i]) c++;
    }
    cout << c ;
}