Cod sursa(job #705827)

Utilizator IronWingVlad Paunescu IronWing Data 4 martie 2012 23:37:00
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <stdio.h>
#include <cmath>

/* function which uses the Prime Sieve of Eratosthenes compute the totient function */
void computeTotient(const int n, int *tot){
	int i, j;
	for(i = 1;i<=n; ++i){
		/*first, set the totient function as if all numbers are prime */
		tot[i] = i-1;
	}
	for(i= 2; i<=n; ++i){
		for(j = 2 * i; j<=n; j += i){
				tot[j] -= tot[i];
		}
	}
}
	
	
long long countFractions(const int n, int *tot){
	long long count = 0;
	int i;
	for(i=2;i<=n;++i){
		count += tot[i];
	}
	return 1 + 2*count;
}

int main(){	
	freopen("fractii.in","r", stdin);
	freopen("fractii.out","w", stdout);
	int n;
	int *tot;
	scanf("%d", &n);
	fclose(stdin);
	tot = new int[n+1](); // allocate memory for the totient function
	computeTotient(n, tot);
	long long fractions = countFractions(n, tot);	
	printf("%lld", fractions);
	delete [] tot;
	fclose(stdout);
	return 0;
}