Cod sursa(job #1567240)

Utilizator Burbon13Burbon13 Burbon13 Data 13 ianuarie 2016 00:00:38
Problema Pairs Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <cstdio>

using namespace std;

const int nmx = 1000002;

int n,MAX;
bool v[nmx],c[nmx];
int nrp[nmx];


void ciur() {
    nrp[0] = 1;
    nrp[1] = 2;
    for(int i = 3; i <= nmx; i += 2)
        if(not c[i]) {
            nrp[++nrp[0]] = i;
            for(int j = 2 * i; j <= nmx; j += i)
                c[j] = true;
        }
}

void input() {
    scanf("%d", &n);
    int nr;
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &nr);
        MAX = MAX < nr ? nr : MAX;
        v[nr] = true;
    }
}

int descompunere(int nr) {
    int total = 0;
    for(int i = 1; nrp[i] * nrp[i] <= nr; ++i)
        if(nr % nrp[i] == 0) {
            ++ total;
            while(nr % nrp[i] == 0)
                nr /= nrp[i];
        }
    if(nr > 1)
        ++ total;
    return total;
}

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

    ciur();

    input();

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

    for(int i = 2; i <= MAX; ++i){
            int t = 0;
            for(int j = i ; j <= MAX; j += i)
                if(v[j])
                    ++ t;
            int d = descompunere(i);
            if(d % 2)
                total -= (t * (t-1)) / 2;
            else
                total += (t * (t-1)) / 2;
        }

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

    return 0;
}