Cod sursa(job #192058)

Utilizator Omega91Nicodei Eduard Omega91 Data 30 mai 2008 15:57:28
Problema Fractii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

inline unsigned long long phi(int x, int k)
{
	return (x - 1) * (double)pow((double)x, (double)(k - 1));
}

unsigned long long totient(int x, bool er[])
{
	int i, k;
	unsigned long long rasp, aux;
	if ( !er[x] ) return x - 1;

	//divizibilitatea la 2
	aux = (x & -x);
	x /= aux;
	aux >>= 1;
	if (aux) rasp = aux;
	else rasp = 1;

	for (i = 3; x != 1; i += 2) {
		if (!er[i] && !(x % i)) {
			k = 1;
			x /= i;
			while ( !(x % i) ) { ++k; x /= i; }
			rasp *= phi(i, k);
		}
	}
	return rasp;
}

int main()
{
	const int NMAX = 1000000;
	bool erathos[NMAX] = {};
	int n, i, j, q;
	unsigned long long s;
	FILE *f;
	f = fopen("fractii.in", "r");
	fscanf(f, "%d", &n);
	fclose(f);

	for (i = 4; i <= n; i += 2) erathos[i] = true;
	q = sqrt(n);
	for (i = 3; i <= q; i += 2)
		if (!erathos[i])
			for (j = i * i; j <= n; j += i) erathos[j] = true;
	s = 1;
	for (i = 2; i <= n; ++i)
		s += totient(i, erathos) * 2;
	f = fopen("fractii.out", "w");
	fprintf(f, "%llu \n", s);
	fclose(f);
	return 0;
}