Cod sursa(job #604802)

Utilizator CristibaluCristi B Cristibalu Data 25 iulie 2011 14:08:10
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.51 kb
#include <stdio.h>
#include <math.h>

int n;
int totient[1000005];

void precalc()
{
	for (int i = 1; i <= n ; ++i)
		totient[i] = i;

	for (int i = 2; i <= n; ++i)
	{
		if (totient[i] == i)
		{
			for (int j = 2*i ; j <=n ; j+=i)
				totient[j] -= totient[j]/i;
			totient[i]--;
		}
	}
}

int main()
{
	freopen("fractii.in","r",stdin);
	freopen("fractii.out","w",stdout);
	scanf("%d",&n);

	precalc();

	long long int rez = 1;
	for (int i = 2 ; i <= n ; ++i)
	{
		rez += 2*totient[i];
	}

	printf("%lld\n",rez);
	return 0;
}