Cod sursa(job #1799839)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 6 noiembrie 2016 21:21:10
Problema Sum Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include<fstream>
#define DIM 100005
using namespace std;
int t, x, i, j, n;
int c[DIM], ok[DIM], nr[DIM];
long long d[DIM];
ifstream fin("sum.in");
ofstream fout("sum.out");
int main(){
    n = 100000;
    for(i = 2; i <= n; i++){
        d[i] = (i + i) * 1LL * (i + i + 1) / 2;
        ok[i] = 1;
    }
    for(i = 2; i <= n; i++){
        if(c[i] == 0){
            d[i] -= 3 * i;
            for(j = i + i; j <= n; j += i){
                c[j] = 1;
                nr[j]++;
                if(j % (i * i) == 0){
                    ok[j] = 0;
                }
                x = (j + j) / i;
                d[j] -= x * 1LL * (x + 1) / 2 * i;
            }
        }
        else{
            if(ok[i] == 1){
                for(j = i; j <= n; j += i){
                    x = (j + j) / i;
                    if(nr[i] % 2 == 1){
                        d[j] -= x * 1LL * (x + 1) / 2 * i;
                    }
                    else{
                        d[j] += x * 1LL * (x + 1) / 2 * i;
                    }
                }
            }
        }
    }
   fin>> t;
    for(; t; t--){
        fin>> x;
        fout<< d[x] <<"\n";
    }
    return 0;
}