Cod sursa(job #2244641)

Utilizator nurof3nCioc Alex-Andrei nurof3n Data 23 septembrie 2018 12:34:41
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <fstream>

using namespace std;

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

const int N_MAX = 1000000;

int totient[N_MAX + 1]; //indicatorul lui Euler

///O(N*lnlnN)
void compute_totient(int N) {
//initializare
    totient[1] = totient[2] = 1;
    for(int i = 3; i <= N; i++)
        totient[i] = i - 1;
//ideea de la ciur
    for(int i = 2; i <= N; i++) //deja stim valoarea totient[1...i]
        for(int j = 2 * i; j <= N; j += i)  //scadem pe totient[i] din totient[k * i]
            totient[j] -= totient[i];
}
int main() {
    int N;
    in >> N;

    compute_totient(N);

    long long sol = 1;
    for(int i = 2; i <= N; i++)
        sol += 2 * totient[i];
    out << sol;

    return 0;
}