Cod sursa(job #1799841)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 6 noiembrie 2016 21:25:59
Problema Sum Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include<fstream>
#include<cstdio>
#define DIM 100005
#define DIM1 1000000
using namespace std;
int t, x, i, j, n, r;
int c[DIM], ok[DIM], nr[DIM];
long long d[DIM];
char s[DIM1 + 5];
FILE * fin = fopen("sum.in", "r");
FILE * fout = fopen("sum.out", "w");
int num(){
    while(s[r] < '0' || s[r] > '9'){
        r++;
        if(r == DIM1){
            r = 0;
            fread(s, 1, DIM1, fin);
        }
    }
    int x = 0;
    while(s[r] >= '0' && s[r] <= '9'){
        x = x * 10 + s[r] - '0';
        r++;
        if(r == DIM1){
            r = 0;
            fread(s, 1, DIM1, fin);
        }
    }
    return x;
}
int main(){
    fread(s, 1, DIM1, fin);
    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;
                    }
                }
            }
        }
    }
    t = num();
    for(; t; t--){
        x = num();
        fprintf(fout, "%lld\n", d[x]);
    }
    return 0;
}