Cod sursa(job #2530780)

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

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

#define debug(x) std::cerr << (#x) << " : " << x << '\n'

const int MV = 1e6 ;

int totient[MV + 5] ;

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

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