Cod sursa(job #2883088)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 1 aprilie 2022 10:14:56
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <bits/stdc++.h>
#define NUMMAX 1000005

using namespace std;

/*******************************/
// INPUT / OUTPUT

ifstream f("pairs.in");
ofstream g("pairs.out");
/*******************************/
/// GLOBAL DECLARATIONS

int N;
long long ans;
int x, maxNum;
bool ciur[NUMMAX], sqrd[NUMMAX];
int dv[NUMMAX], nr[NUMMAX];
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> N;

    for (int i = 1 ; i <= N ; ++ i)
    {
        f >> x;
        dv[x] ++;
        maxNum = max(maxNum, x);
    }
}
///-------------------------------------
inline void Ciur()
{
    for (int i = 2 ; 2 * i <= maxNum ; ++ i)
    {
        if (!ciur[i]) nr[i] = 1;
        for (int j = 2 * i ; j <= maxNum ; j += i)
        {
            dv[i] += dv[j];
            if (!ciur[i])
            {
                nr[j] ++;
                ciur[j] = true;
                if (j % (i * i) == 0) sqrd[j] = false;
            } 
        }
    }
}
///-------------------------------------
inline void Solve()
{
    long long numPairs;
    ans = 1LL * N * (N - 1) / 2;
    for (int i = 2 ; i <= maxNum ; ++ i)
    {
        if (!sqrd[i])
        {
            numPairs = 1LL * dv[i] * (dv[i] - 1) / 2;
            if (nr[i] % 2 == 1) ans -= numPairs;
            else ans += numPairs;
        }
    }
}
///-------------------------------------
inline void Solution()
{
    Ciur();
    Solve();
}
///-------------------------------------
inline void Output()
{
    g << ans;
}
///-------------------------------------
int main()
{
    ReadInput();
    Solution();
    Output();
    return 0;
}