Cod sursa(job #3273811)

Utilizator anon2718281828Iasmina Matei anon2718281828 Data 3 februarie 2025 21:57:03
Problema Fractii Scor 100
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; i <= n / 2; i++) {
        if (nonPrimes[i].empty()) {
            for (int j = 2; j <= n / i; j++) {
                nonPrimes[i * j].push_back(i);
            }
        }
    }

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

    out << sol;

    return 0;
}