Pagini recente » Cod sursa (job #1276781) | Cod sursa (job #1527347) | Cod sursa (job #1791922) | Statistici Michel-Daniel Cojocaru (Cojocaru_Michel_Daniel_324CB) | Cod sursa (job #1595701)
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
ifstream in("fractii.in");
ofstream out("fractii.out");
int n, m = 1;
bool *primes;
void citire(){
in >> n;
primes = new bool[n + 1];
fill_n(primes, n + 1, true);
}
int binary_gcd(int u, int v){
int shift;
if (u == 0) return v;
if (v == 0) return u;
for (shift = 0; ((u | v) & 1) == 0; ++shift) {
u >>= 1;
v >>= 1;
}
while ((u & 1) == 0)
u >>= 1;
do {
while ((v & 1) == 0)
v >>= 1;
if (u > v) { int t = v; v = u; u = t;}
v = v - u;
} while (v != 0);
return u << shift;
}
int phi(int k){
if ( n < 2 )
return 1;
if ( primes[k] )
return k-1;
if ( (k & 1) == 0 ){
int m = k >> 1;
return !(m & 1) ? phi(m)<<1 : phi(m);
}
for ( int p = 2; p <= n; p++){
if (!primes[p]) continue;
if ( n % p ) continue;
int o = n/p;
int d = binary_gcd(p, o);
return d==1? phi(p)*phi(o) : phi(p)*phi(o)*d/phi(d);
}
}
void rezolvare(){
for(int i = 2; i <= sqrt(n); i++){
if(primes[i])
for(int j = i; j <= n / i; j++){
primes[j * i] = false;
}
}
for(int i = 2; i <= n; i++){
m += 2 * phi(i);
}
}
void scriere(){
out << m;
}
int main()
{
citire();
rezolvare();
scriere();
return 0;
}