Cod sursa(job #2261445)

Utilizator osiaccrCristian Osiac osiaccr Data 16 octombrie 2018 11:13:55
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 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;
    }

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

    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] == -1) {
            continue;
        }

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

    }

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

    return 0;
}