Cod sursa(job #1412000)

Utilizator BabutaRaresBabuta Rares Mihai BabutaRares Data 1 aprilie 2015 02:21:05
Problema Fractii Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<cmath>
using namespace std;

int a[100][1000001], totient[1000001], i, j, s, k, n,  e;
long long sol = 1;

int main()
{
	FILE *in = fopen("fractii.in", "r"), *out = fopen("fractii.out", "w");
	fscanf(in, "%d", &n);
	totient[1] = 1;
	for (i = 2; i <= n; i++)
	{
		if (i == 3)
		{
			n = n;
		}
		if (a[0][i] == 0)
		{
			totient[i] = i - 1;
			for (j = i + i; j <= n; j = j + i)
			{
				a[0][j] = 1;
				a[1][j]++;
				a[a[1][j]+1][j] = i;
			}
		}
	}
	
	for (i = 2; i <= n; i++)
	{
		if (a[0][i] == 0) sol += (totient[i]*2);
		else
		{
			s = 1; k = i; e = 0;
			for (j = 2; j <= a[1][i] +1; j++)
			{
				e = 0;
				while (k%(a[j][i]) == 0)
				{
					e++;
					k /= (a[j][i]);
				}
				s *= (pow(a[j][i], e) - pow(a[j][i], e - 1));
			}
			sol += s * 2;
			
		}
	}
	fprintf(out, "%d", sol);
}