Cod sursa(job #1644356)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 9 martie 2016 22:40:47
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 1e6 + 10;

int n;
int phi[Nmax];
bool s[Nmax];
vector < int > v;

void bagaciur()
{
    for (int i = 2; i * i <= n; ++i)
    {
        if (s[i] == 1) continue;
        for (int j = i * 2; j <= n; j += i)
            s[j] = 1;
    }

    for (int i = 2; i <= n; ++i)
    {
        if (s[i] == 1) continue;
        v.push_back(i);
    }
}

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

    scanf("%d", &n);
    bagaciur();

    phi[1] = 1;
    for (int x = 2; x <= n; ++x)
    {
        for (auto &it : v)
        {
            if (it * it > x) break;
            if (x % it) continue;

            int y = x , p = 1;
            while (y % it == 0)
                y /= it, p *= it;

            ///it^(exp-1) * (it-1)

            phi[x] = phi[y] * p/it * (it - 1);
            break;
        }

        if (!phi[x]) phi[x] = x - 1;
    }

    long long sum = 0LL;
    for (int i = 1; i <= n; ++i) sum += phi[i];
    sum <<= 1LL; sum--;
    printf("%lld\n", sum);

    return 0;
}