Cod sursa(job #2261387)

Utilizator circeanubogdanCirceanu Bogdan circeanubogdan Data 16 octombrie 2018 10:40:55
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <fstream>
#define DIM 100005
#define DIMC 1000000

using namespace std;

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

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

bool viz[DIMC + 10];

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 = 2; 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;
}