Cod sursa(job #1972805)

Utilizator RaresEGaySopterean Adrian RaresEGay Data 23 aprilie 2017 18:58:38
Problema Fractii Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <math.h>

#define MAXN 1000005

using namespace std;

ifstream f ("fractii.in");
ofstream g ("fractii.out");

int n;
int c[MAXN];

double eulerTotient(int x){
    double p = x;
    if(x % 2 == 0){
        p /= 2;
        while(x % 2 == 0) x /= 2;
    }
    for(int i = 3; x > 1; i += 2){
        if(x % i == 0){
            p *= (i - 1);
            p /= i;
            while(x % i == 0) x /= i;
        }
    }

    return p;
}

int main(){
    f >> n;
    for(int i = 2; i <= n; ++i){
        if(!c[i]){
            for(int j = 2 * i; j <= n; j += i)
                c[j] = 1;
        }
    }
    long long sol = 1;
    for(int i = 2; i <= n; ++i){
        if(!c[i]) sol += 2 * (i - 1);
        else sol += 2 * eulerTotient(i);
    }

    g << sol << '\n';
}