Cod sursa(job #3258704)

Utilizator raduiliIliusco Radu raduili Data 23 noiembrie 2024 13:04:13
Problema Sum Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <iostream>
#include <fstream>
using namespace std;

#define LL long long

ifstream fin("sum.in");
ofstream fout("sum.out");

/*
int phi(int n) {
    int tot[n+1];
    for (int i=1; i<=n; i++) {
        tot[i] = i;
    }

    for (int i=2; i<=n; i++) {
        if (tot[i] == i) {
            tot[i]--;  // phi(i) = i-1 daca i prim
            for (int j=2; i*j <= n; j++) {
                tot[i*j] = tot[i*j]/i * (i-1);
            }
        }
    }

    return tot[n];
}
*/

const int n = 100000;
int tot[n+1];

int main()
{
    int a; fin >> a;

    // calculez dinainte tot pana la nr maxim din problema (100 000)
    for (int i=1; i<=n; i++) {
        tot[i] = i;
    }

    for (int i=2; i<=n; i++) {
        if (tot[i] == i) {
            tot[i]--;  // phi(i) = i-1 daca i prim
            for (int j=2; i*j <= n; j++) {
                tot[i*j] = tot[i*j]/i * (i-1);
            }
        }
    }

    // rezolvare
    for (int i=0; i<a; i++) {
        LL x; fin >> x;
        LL s = (LL)tot[x] * x * (LL)2;
        fout << s << endl;
    }

    return 0;
}