Mai intai trebuie sa te autentifici.

Cod sursa(job #1254321)

Utilizator harababurelPuscas Sergiu harababurel Data 2 noiembrie 2014 15:40:45
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <iostream>
#include <fstream>
#define ll long long
#define nmax 1000005
using namespace std;

ll n, sol, phi[nmax], P[nmax], Q[nmax];
bool prime[nmax];

int main() {
    ifstream f("fractii.in");
    ofstream g("fractii.out");

    f>>n;
    for(int i=1; i<=n; i++) phi[i] = i, prime[i] = true, P[i] = Q[i] = 1LL;

    for(int i=2; i<=n; i++) {
        if(!prime[i]) continue;
        for(int j=i; j<=n; j+=i) {
            if(j != i) prime[j] = false;
            P[j] *= i-1;
            Q[j] *= i;
        }
    }

    for(int i=2; i<=n; i++) {
        phi[i] *= P[i];
        phi[i] /= Q[i];
        sol += 1LL*phi[i];

    }

    sol = 2LL*sol + 1LL;

    g<<sol<<"\n";
    return 0;
}