Cod sursa(job #2520360)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 9 ianuarie 2020 12:55:12
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <bits/stdc++.h>
#define VALUEMAX 1000005

using namespace std;

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

long long n , x;
int fr[VALUEMAX] , nrdiv[VALUEMAX];
bool a[VALUEMAX];

int main()
{
    long long i = 0 , j = 0;
    int xmax = 0;

    f >> n;

    long long ans = n * (n - 1) / 2 , cnt = 0;

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

    for(i = 2 ; i <= xmax ; i++)
    {
        if(nrdiv[i] == 0)
        {
            for(j = i ; j <= xmax ; j += i)
            {
                ++nrdiv[j];

                if(j % (i * i) == 0)
                    a[j] = 1;
            }
        }

        if(a[i] == 0)
        {
            cnt = 0;

            for(j = i ; j <= xmax ; j += i)
                cnt += fr[j];

            if(nrdiv[i] % 2 == 1)
                ans -= cnt * (cnt - 1) / 2;
            else ans += cnt * (cnt - 1) / 2;
        }
    }

    g << ans;

    return 0;
}