Cod sursa(job #557798)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 16 martie 2011 21:17:30
Problema Ciurul lui Eratosthenes Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
# include <fstream.h>
# include <time.h>
long n;
time_t t1, t2;
char a[(10000000 >> 3) + 1];
ifstream f ("ciur.in");
ofstream g ("ciur.out");
void ciur_bazat (){
	long sol = 1;
	for (long j = 4; j <= n; j += 2, a[j >> 3] |= (1 << (j & 7) ) );
	for (long i = 3; i <= n; i += 2){
		if (!(a[i >> 3] & (1 << (i & 7) ) ) ){
			for (long j = i * 3, x = i << 1; j <= n; j += x) a[j >> 3] |= (1 << (j & 7) );
			++sol;
		}
	}
	g << sol << '\n';
}
int main (){
	t1 = clock ();
	f >> n; ciur_bazat ();
	t2 = clock ();
	g << (double)(t2 - t1) / 1000;
	return 0;
}