Cod sursa(job #2698730)

Utilizator PopescuAndreiAlexandruPopescu Andrei Alexandru PopescuAndreiAlexandru Data 22 ianuarie 2021 21:30:48
Problema Fractii Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 1000000;

int seen[N+5],Prime[N+5],n,p;

long long sum=1;

void Eratostene()
{
    seen[1]=1;
    for(int i=2; i*i<=N; i++)
        for(int j=i+i; i*j<=N; j+=i)
            seen[j]=1;
}

void GetPrime()
{
    for(int i=1; i<=N; i++)
        {
            if(!seen[i])
                {
                    n++;
                    Prime[n]=i;
                }
        }
}

int EulerTotient(int k)
{
    int p=0,cnt=1,d=Prime[1],ans=1;
    while(k>1)
        {
            p=0;
            while(k%d==0)
                {
                    p++;
                    k/=d;
                }
            if(p)
                {
                    ans*=(d-1);
                    for(int i=1; i<p; i++)
                        ans*=d;
                }
            cnt++;
            d=Prime[cnt];
            if(k>1 && d*d>k)
                d=k;
        }
    return ans;
}

void Print()
{
    for(int i=2; i<=p; i++)
        sum+=EulerTotient(i)*2;
    fout<<sum;
}

int main()
{
    /*
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    */
    Eratostene();
    GetPrime();
    fin>>p;
    Print();
}