Cod sursa(job #1689299)

Utilizator medicinedoctoralexandru medicinedoctor Data 14 aprilie 2016 09:19:41
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <cmath>
using namespace std;

bool a[2000000];
long n;

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

long cc()
{
	long x=0;
	for (long i=1; i<=n; i++)
		if (a[i]) x++;
	return x;
}

void lire()
{
	ifstream f("ciur.in");
	f >> n;
	f.close();
}

void ecrire(long x)
{
	ofstream f("ciur.out");
	f << x;
	f.close();
}

int main()
{
	lire();
	prime();
	ecrire(cc());
	return 0;
}