Cod sursa(job #1265292)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 17 noiembrie 2014 01:36:01
Problema Fractii Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <cstdio>

using namespace std;
int N,phi[1000005];
long long ans;


long long lg_put(long long a,long long b)
{
    long long x1 = a,x2 = 1;
    if( b == 0 ) return 1;
    if( b == 1 ) return a;
    while(b > 1){
        if(b & 1){
            x2 = x1 * x2;
            b ^= 1;
        }
        else
        {
            x1 = x1 * x1;
            b >>= 1;
        }
    }
    return x1 * x2;
}
long long fi(int x){
    int d = 2,p = 0,rez = 1;
    while(x > 1){
        if(x % d == 0)
        {
            while(x % d == 0){
                ++p;
                x/=d;
            }
            rez *= (d - 1) * lg_put(d,p - 1);
        }
        ++d;
        p = 0;
    }
    return rez;
}

void solve()
{
    for(int i = 2; i <= N; ++i)
        phi[i] = fi(i);
    for(int i = 2; i <= N; ++i)
        ans += 2*phi[i];
    ans ++;
}

int main()
{
    freopen("fractii.in","r",stdin);
    freopen("fractii.out","w",stdout);

    scanf("%d",&N);
    solve();
    printf("%lld\n",ans);

    return 0;
}