Cod sursa(job #610220)

Utilizator IgnitionMihai Moraru Ignition Data 25 august 2011 19:01:37
Problema Fractii Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.8 kb
/*
 * 002-Fractii
 * 2011-08-25
 *
 * We apply the formula n = sum(d divides n, phi(d))
 * From here we have that phi(n) = n - sum(d divides n && d != n, phi(d));
 *
 * It's a tad slower than the other method for computing phi, the one with products, but uses less memory since it doesn't actually compute prime numbers (you'll notice it doesn't use Eratosthenes' xieve).
*/

#include <stdio.h>

#define MN (1000001)


int main()
{
	int N;
	int phi[MN]; // Euler's totient function
	int i, j;
	long long S = 0;

	freopen("fractii.in", "r", stdin);
	freopen("fractii.out", "w", stdout);
	scanf("%d", &N);

	for(i = 1; i <= N; ++i)
		phi[i] = i-1;

	for(i = 2; i <= N; ++i) {
		for(j = i+i; j <= N; j += i)
			phi[j] -= phi[i];
	}

	for(i = 2; i <= N; ++i)
		S += phi[i];
	S = 2*S+1;

	printf("%lld\n", S);

	return 0;
}