Cod sursa(job #2276156)

Utilizator TooHappyMarchitan Teodor TooHappy Data 4 noiembrie 2018 11:49:14
Problema Fractii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>
using namespace std;
 
ifstream in("fractii.in");
ofstream out("fractii.out");

const int Nmax = 1e6 + 10;

int euler[Nmax];
bool notPrime[Nmax];

int main() {
    ios::sync_with_stdio(false); in.tie(0); out.tie(0);

    int n; in >> n;

    for(int i = 2; i <= n; ++i) {
        euler[i] = i;
    }

    for(int i = 2; i <= n; i += 2) {
        euler[i] -= euler[i] / 2;
    }

    for(int i = 3; i <= n; i += 2) {
        if(notPrime[i]) {
            continue;
        }

        for(int j = i; j <= n; j += i) {
            notPrime[j] = true;
            euler[j] -= euler[j] / i;
        }
    }

    long long ans = 0;
    for(int i = 2; i <= n; ++i) {
        ans += euler[i];
    }

    out << 2 * ans + 1 << "\n";

    in.close(); out.close();

    return 0;
}