Cod sursa(job #3266736)

Utilizator xiaopangXiaopang Hue xiaopang Data 10 ianuarie 2025 08:45:36
Problema Fractii Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

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

int n;

void compute_totients(int n, vector<int>& phi) {
    for (int i = 1; i <= n; ++i) {
        phi[i] = i;  // Initialize each phi[i] to i
    }

    for (int i = 2; i <= n; ++i) {
        if (phi[i] == i) {  // i is a prime number
            for (int j = i; j <= n; j += i) {
                phi[j] = phi[j] * (i - 1) / i;  // Apply the sieve formula
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);

    f >> n;

    // Initialize the vector to store Euler's Totient values
    vector<int> phi(n + 1);

    // Precompute the Euler's Totient function for all values from 1 to n
    compute_totients(n, phi);

    unsigned long long rasp = 1;

    // Sum up 2 * Euler(i) for each i from 2 to n
    for (int i = 2; i <= n; ++i) {
        rasp += 2 * phi[i];
    }

    g << rasp;

    return 0;
}