Cod sursa(job #3273809)

Utilizator anon2718281828Iasmina Matei anon2718281828 Data 3 februarie 2025 21:52:28
Problema Fractii Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;

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

int main() {
    int n;
    in >> n;

    vector<vector<int>> nonPrimes(n + 1);
    for (int i = 2; 1LL * i <= 1LL * n / 2; i++) {
        if (nonPrimes[i].empty()) {
            for (int j = 2; j <= n / i; j++) {
                nonPrimes[i * j].push_back(i);
            }
        }
    }

    int sol = 1;
    for (int i = 2; i <= n; i++) {
        if (nonPrimes[i].empty()) {
            sol += (i - 1) * 2;
        } else {
            int prod = i;
            for (const auto& p: nonPrimes[i]) {
                prod = prod * (p - 1) / p;
            }
            sol += prod * 2;
        }
    }

    out << sol;

    return 0;
}