Cod sursa(job #1982987)

Utilizator medicinedoctoralexandru medicinedoctor Data 20 mai 2017 21:00:08
Problema Sum Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>

using namespace std;

int gcd(int a, int b) {
    if (b != 0) {
        return gcd(b, a % b);
    }

    return a;
}

bool f(int a, int b) {
    return (a % b != 0);
}

bool ok(int a, int b) {
    if (a > b) {
        return f(a, b);
    } else {
        return f(b, a);
    }
}

long long calc(int x) {
    long long s = 1;

    for (int i = 2; i <= 2 * x; i++) {
        if (ok(i, x) && gcd(i, x) == 1) {
            s += i;
        }
    }

    return s;
}

int main() {
    ifstream cin ( "sum.in");
    ofstream cout("sum.out");

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        cout << calc(x) << '\n';
    }

    cin. close();
    cout.close();

    return 0;
}