Cod sursa(job #2530347)

Utilizator rares404AlShaytan - Balasescu Rares rares404 Data 24 ianuarie 2020 18:10:02
Problema Fractii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma unroll

std::fstream fin("pietre.in", std::ios::in) ;
std::fstream fout("pietre.out", std::ios::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 ;
}