Cod sursa(job #2261451)

Utilizator osiaccrCristian Osiac osiaccr Data 16 octombrie 2018 11:18:24
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>
#include <vector>
#include <bitset>
#define DEF 100010
#define MAX 1000010

using namespace std;

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

long long Max, n, Res;

bitset < MAX > B;

int Ciur[MAX];

int main () {

    fin >> n;

    for (int i = 1; i <= n; ++ i) {
        int x;
        fin >> x;
        if (x > Max)
            Max = x;
        B[x] = 1;
    }


    //Ciur
    for (int i = 2; i <= Max; ++ i) {
        Ciur[i] = 1;
        for (int j = 1; j * i <= Max; ++ j) {
            if (Ciur[i] == 0) {
                ++ Ciur[i * j];
            }
        }
    }

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

    //Calculare
    for (int i = 2; i <= Max; ++ i) {

        long long val = 0;
        for (int j = 1; j * i <= Max; ++ j) {
            val += B[i * j];
        }

        if (Ciur[i] % 2 == 0) {
            Res -= val * (val - 1) / 2;
        }
        else {
            Res += val * (val - 1) / 2;
        }

    }

    fout << n * (n - 1) / 2 - Res;

    return 0;
}