Cod sursa(job #2546427)

Utilizator Alex_AeleneiAlex Aelenei Ioan Alex_Aelenei Data 14 februarie 2020 10:23:47
Problema Fractii Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <cmath>
#include <fstream>
#include <iostream>

using namespace std;

const int NMAX = 1e6 ;
int ind [ NMAX + 5 ] ;

inline void pregen ( int n )
{
    int i , j , lim = ( int ) sqrt ( ( double ) n ) ;

    ind [ 0 ] = 1 ;
    for ( i = 1 ; i <= n ; ++ i )
        ind [ i ] = i ;

    ind [ 2 ] = 1 ;
    for ( i = 4 ; i <= n ; i += 2 )
        ind [ i ] /= 2 ;

    for ( i = 3 ; i <= lim ; i += 2 )
        if ( ind [ i ] == i )
        {
            -- ind [ i ] ;

            for ( j = 2 * i ; j <= n ; j += i )
                ind [ j ] = ( ind [ j ] / i ) * ( i - 1 ) ;
        }
}

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

    int n , i ;
    long long ans = 1 ;
    in >> n ;

    pregen ( NMAX ) ;

    for ( i = 2 ; i <= n ; ++ i )
        ans = ans + 2 * ind [ i ] ;

    out << ans ;
    return 0;
}