Cod sursa(job #2260710)

Utilizator circeanubogdanCirceanu Bogdan circeanubogdan Data 15 octombrie 2018 14:21:47
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#define DIM 100002
#define DIMC 1000002

using namespace std;

ifstream in ("pairs.in");
ofstream out("pairs.out");

int n, maxim = -1;
int ciur[DIMC];

bool viz[DIMC];

long long notPrime;

void CalcCiur(){
    ciur[2] = 1;
    for(int i = 2; i * i < DIMC; ++ i){
        if(!ciur[i]){
            ciur[i] = 1;
            for(int j = i + i; j < DIMC; j += i){
                ++ ciur[j];
            }
        }
    }

    for(int i = 2; i * i < DIMC; ++ i){
        for(int j = i * i; j < DIMC; j += i * i)
            ciur[j] = 0;
    }
}

int main()
{
    in>>n;
    int x = 0;
    for(int i = 1; i <= n; ++ i){
        in>>x;
        viz[x] = 1;
        maxim = max(x, maxim);
    }
    CalcCiur();
    long long nr = 0;
    for(int i = 1; i < DIMC; ++ i){
        if(ciur[i]){
            for(int j = i; j < DIMC; j += i)
                nr += viz[j];

            if(ciur[i] % 2){
                notPrime += 1LL * nr * (nr - 1) / 2LL;
            }
            else{
                notPrime -= 1LL * nr * (nr - 1) / 2LL;
            }
        }
        nr = 0;
    }
    out<<1LL * n * (n - 1) / 2LL - notPrime;
    return 0;
}