Cod sursa(job #2271425)

Utilizator ClaudiuTClaudiu Timofte ClaudiuT Data 28 octombrie 2018 15:56:36
Problema Fractii Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std ;

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

int N ;
bool Nr_Prime[1000000] ;

void Eratostene(bool Nr_Prime[])
{
    for(int i = 1 ; i <= 999999 ; i += 1)
        Nr_Prime[i] = true ;

    for(int i = 2 ; i <= N ; i += 1)
    {
        if(Nr_Prime[i])
        {
            for(int k = i * i ; k <= 999999 ; k += i)
                Nr_Prime[k] = false ;
        }
    }
}

float Euler(int x)
{
    float nr = x ;

    for(int i = 2 ; i <= x ; i += 1)
    {
        if(Nr_Prime[i] && !(x % i))
            nr *= (1.0 - 1.0 / i) ;
    }

    return nr ;
}

void Function()
{
    Eratostene(Nr_Prime) ;

    int nr = 0 ;

    for(int i = 2 ; i <= N ; i += 1)
    {
        nr += 2 * Euler(i) ;
    }

    fout << nr + 1 ;

}

int main()
{
    ios_base::sync_with_stdio(false) ;
    fin.tie(NULL) ;

    fin >> N ;

    Function() ;


    return 0 ;
}