Cod sursa(job #3258694)

Utilizator raduiliIliusco Radu raduili Data 23 noiembrie 2024 12:55:54
Problema Sum Scor 55
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 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];
}

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

    for (int i=0; i<n; i++) {
        int x; fin >> x;
        LL s = (LL)phi(x) * (LL)x * (LL)2;
        fout << s << endl;
        cout << s << endl;
    }

    return 0;
}