Cod sursa(job #2815829)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 10 decembrie 2021 14:50:43
Problema Pairs Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int maxN = 1000000;
int n, maxi, nr[maxN + 5];
long long ans;
bool v[maxN + 5], np[maxN + 5], pow[maxN + 5];

void ciur()
{
    for(int i = 2; i <= maxN; i++)
    {
        if(!np[i])
        {
            nr[i]=1;
            pow[i]=0;
            for(int j = 2 * i; j <= maxN; j += i)
            {
                np[j]=1;
                nr[j]++;
                if((j / i) % i == 0)
                    pow[j]=1;
            }
        }
    }
}

int main()
{
    int a;
    fin >> n;
    for(int i = 1; i <= n; i++)
    {
        fin >> a;
        v[a] = 1;
        maxi = max(maxi, a);
    }
    ciur();
    for(int i = 2; i <= maxi; i++)
    {
        if(pow[i])
            continue;
        int x = 0;
        for(int j = i; j <= maxi; j+=i)
        {
            x += v[j];
        }
        if(nr[i] % 2 == 1)
            ans += 1LL * x * (x - 1)/2;
        else
            ans -= 1LL * x * (x - 1)/2;
    }
    ans = 1LL * n * (n - 1) / 2 - ans;
    fout << ans << '\n';
    return 0;
}