Cod sursa(job #2792941)

Utilizator DragosC1Dragos DragosC1 Data 2 noiembrie 2021 15:51:44
Problema Sum Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
	
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
 
int n;
int a[100001];
 
void read() {
    int i;
    ifstream f("sum.in");
    f >> n;
    for (i = 1; i <= n; i++)
        f >> a[i];
    f.close();
}
 
vector<int> fact[100001];
bitset<100001> e;
 
void solve() {
    int i, j;
    for (i = 2; i <= 100000; i++)
        if (!e[i]) {
            fact[i].emplace_back(i);
            for (j = 2 * i; j <= 100000; j += i) {
                fact[j].emplace_back(i);
                e[j] = 1;
            }
        }
}
 
void output() {
    int i, j, x, nr, P, k, aux;
    long long sum;
    ofstream g("sum.out");
    for (i = 1; i <= n; i++) {
        x = a[i];
        sum = 1LL * 2 * x * (2 * x + 1) / 2;
        for (j = 1; j < (1 << fact[x].size()); j++) {
            nr = 0, P = 1;
            for (k = 0; k < fact[x].size(); k++)
                if (j & (1 << k)) {
                    nr++;
                    P *= fact[x][k];
                }
            aux = 2 * x / P;
            if (nr & 1)
                sum -= 1LL * P * aux * (aux + 1) / 2;
            else sum += 1LL * P * aux * (aux + 1) / 2;
        }   
        g << sum << '\n';
    }
    g.close();
}
 
int main() {
    read();
    solve();
    output();
    return 0;
}