Cod sursa(job #2927122)

Utilizator antonio.potraPotra Paul-Antonio antonio.potra Data 19 octombrie 2022 16:17:16
Problema Fractii Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>

void ComputeTotientFunction(int phi[], int n) {
    for (int i = 1; i <= n; i++) { // initialisation
        phi[i] = i;
    }
    phi[1] = 0;
    for (int i = 2; i <= n; i++) {
        if (phi[i] == i) {
            phi[i]--; // decrement the value so that it is correct for prime numbers
            for (int j = 2; j * i <= n; j++) { // go through all the multiples of i
                phi[j * i] = phi[j * i] / i * (i - 1); // update their value
            }
        }
    }
}

int main() {
    std::ifstream fin("fractii.in");
    std::ofstream fout("fractii.out");

    int n;
    fin >> n;

    int phi[n + 1]; // Euler's totient function, phi[i] = # of numbers < i that are relatively prime to i
    ComputeTotientFunction(phi, n);

    int result = 0;
    for (int i = 1; i <= n; i++) {
        result += phi[i] * 2; // fractions can be reversed, so multiply by 2
    }
    fout << result + 1; // add the fraction 1 / 1

    return 0;
}