Cod sursa(job #2768719)

Utilizator DragosC1Dragos DragosC1 Data 11 august 2021 22:43:41
Problema Pairs Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

int fr[1000001];
int nrdiv[1000001];
bool ok[1000001];
int n;
int Max;
long long rez;

void read() {
    int i, x;
    ifstream f("pairs.in");
    f >> n;
    for (i = 1; i <= n; i++) {
        f >> x;
        if (x > Max)
            Max = x;
        fr[x]++;
    }
    f.close();
}

void solve() {
    int i, j, nr, x;

    for (i = 2; i <= Max; i++)
        ok[i] = 1;

    for (i = 2; i <= Max; i++) 
        if (!nrdiv[i]) 
            for (j = i; j <= Max; j += i) {
                nrdiv[j]++;
                if (j % (i * i) == 0)
                    ok[j] = 0;
            }

    rez = n * (n - 1) / 2;
    for (i = 2; i <= Max; i++) 
        if (ok[i]) {
            nr = min(1, fr[i]);
            for (j = 2; j * i <= Max; j++)
                nr += fr[i * j];
            if (nrdiv[i] & 1)
                rez -= nr * (nr - 1) / 2;
            else rez += nr * (nr - 1) / 2;
        }        
}

void output() {
    ofstream g("pairs.out");
    g << rez;
    g.close();
}

int main() {
    read();
    solve();
    output();
    return 0;
}