Cod sursa(job #2530783)

Utilizator rares404AlShaytan - Balasescu Rares rares404 Data 25 ianuarie 2020 12:11:23
Problema Fractii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>

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

const int MV = 1e6 ;

long long totient[MV + 5] ;

void pre(long long n) {
        for(long long p = 1 ; p <= n ; ++ p) {
                totient[p] = p ;
        }
        for(long long i = 2 ; i <= n ; ++ i) {
                if (totient[i] == i) {
                        totient[i] -- ;
                        for (long long j = i + i ; j <= n ; j += i) {
                                totient[j] = totient[j] / i * (i - 1) ;
                        }
                }
        }
}

int main () {
        long long n ; fin >> n ;
        pre(n) ;
        long long ans(1) ;
        for (long long k = 2 ; k <= n ; ++ k) {
                ans += (totient[k] * 2) ;
        }
        fout << ans ;
}