Cod sursa(job #953360)

Utilizator Impaler_009Mihai Nitu Impaler_009 Data 25 mai 2013 20:35:15
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
using namespace std;
ifstream fin("fractii.in");
ofstream fout("fractii.out");

bool v[1000001]; // 0 if prime, 1 if composite
int totient [1000001];
//Problema cere in fond toate perechile de numere i,j <= N cu i,j prime intre ele. Folosim deci indicatorul lui Euler
//si, pentru mai multa rapiditate, ciurul lui eratostene pentru a gasi numerele prime (indicatorul lui euler pentru n prim este automat n-1)


void erathostenes (int n)
{
    for (int i=4;i<=n;i=i+2) v[i]=1;
    for (int i=3;i*i<=n;i=i+2)
    {
        if (v[i]) continue;
        for (int j=i; j*i<=n; j++) v[j*i]=1;
    }
}

int main()
{
    int n; long long f=0;
    fin>>n;
    erathostenes (n);
    for (int i=2; i<=n; i++)
    {
        if (!v[i]) totient[i]=i-1;
        else
        {
            int j;
            for (j=2; i%j ; j++);
            if (i/j%j==0) totient[i]=totient[i/j]*j;     //reccurence for totient
            else totient[i]=totient[i/j]*(j-1);
        }
        f+=totient[i];
    }
    f=f*2; f++;                   //pentru o fractie x/y ireductibila, exista si fracti y/x ireductibila. De asemenea sa nu uitam de 1/1
    fout<<f;
}