Cod sursa(job #2857197)

Utilizator DooMeDCristian Alexutan DooMeD Data 25 februarie 2022 08:49:56
Problema Fractii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.53 kb
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e6;

int phi[nmax+5];

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

    int n; f >> n;
    for(int i=1; i<=n; i++) phi[i] = i;
    for(int i=2; i<=n; i++) {
        if(phi[i]==i) {
            phi[i] = i - 1;
            for(int j=i+i; j<=n; j+=i)
                phi[j] = phi[j] / i * (i-1);
        }
    }
    long long nr = 1;
    for(int i=2; i<=n; i++)
        nr = nr + 2LL * phi[i];
    g << nr;
    return 0;
}