Cod sursa(job #2774241)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 10 septembrie 2021 17:44:25
Problema Pairs Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>
#define DIM 1000005

using namespace std;

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

int n, nrDivImp[DIM], fr[DIM], maxi;
bitset <DIM> v;

int main()
{
    f >> n;
    for(int i = 1; i <= n; i++)
    {
        int x;
        f >> x;
        fr[x]++;
        maxi = max(maxi, x);
    }

    long long rez = n * (n - 1) / 2;
    for(int i = 2; i <= maxi; i++)
        if(!v[i])
        {
            long long cnt = 0 ;
            bool isPrim = 0;
            if(!nrDivImp[i])
                isPrim = 1;
            for(int j = i; j <= maxi; j += i)
            {
                if(isPrim)
                {
                    nrDivImp[j]++;
                    if(j % (i * i) == 0)
                        v[j] = 1;
                }
                cnt += fr[j];
            }
            if(!(nrDivImp[i] % 2))
                rez += cnt * (cnt - 1) / 2;
            else
                rez -= cnt * (cnt - 1) / 2;
        }

    g << rez;
    return 0;
}