Cod sursa(job #2261393)

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

using namespace std;

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

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

int 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]){
            nr = 0;
            for(int j = i; j <= DIMC; j += i)
                nr += viz[j];

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

    }
    out<<1LL * (long long)n * (long long)((long long)n - 1) / (1LL * 2LL) - notPrime;
    return 0;
}