Cod sursa(job #1112898)

Utilizator krisztian1997Kristo Krisztian krisztian1997 Data 20 februarie 2014 09:48:40
Problema Fractii Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
using namespace std;
long long int sumatotiene;
ifstream f("fractii.in");
ofstream g("fractii.out");

long long int fi(long long int n) 
{ 
       long long int result = n; 
       for(int i=2;i*i <= n;i++) 
       { 
         if (n % i == 0) result -= result / i; 
         while (n % i == 0) n /= i; 
       } 
       if (n > 1) result -= result / n; 
       return result; 
} 
/* totiente optimizat */
long long int totient(long long int n){
long long int tot = n; //this will be the totient at the end of the sample
for (int p = 2; p*p <= n; p++)
{
    if (n%p==0)
    {
        tot /= p;
        tot *= (p-1);
        while ( n % p == 0 ) 
            n /= p;
    }
}
if ( n > 1 ) { // now n is the largest prime divisor
    tot /= n;
    tot *= (n-1);
}  
return tot;
}
int main(){
	int n;
	f>>n;
	for(int i = 2; i <= n; i++){
		sumatotiene += totient(i);
	}
	long long int final = 1 + (2*sumatotiene);
	g << final;
	return 0;
}