Cod sursa(job #1567576)

Utilizator Burbon13Burbon13 Burbon13 Data 13 ianuarie 2016 15:53:46
Problema Pairs Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <cstdio>

using namespace std;

const int maxx = 1000000;

int nr[1000005],n,aux;
long long total;
bool a[1000005  ];

int main() {
    freopen("pairs.in", "r", stdin);
    freopen("pairs.out", "w", stdout);

    for(int i = 2; i <= maxx; ++i)
        if(not nr[i])
            for(int j = i; j <= maxx; j += i)
                ++ nr[j];

    for(int i = 2; i * i <= maxx; ++ i)
        for(int j = i * i; j <= maxx; j += i*i)
            nr[j] = 0;

    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &aux);
        a[aux] = true;
    }

    total = (1LL*n*n-n)/2;

    for(int i = 2; i <= maxx; ++i)
        if(nr[i]) {
            aux = 0;
            for(int j = i; j <= maxx; j += i)
                if(a[j])
                    ++ aux;
            if(nr[i] % 2)
                total -= (1LL*aux*aux-aux)/2;
            else
                total += (1LL*aux*aux-aux)/2;
        }

    printf("%lld\n", total);

    return 0;
}