Cod sursa(job #3290993)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 2 aprilie 2025 20:04:05
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>
#include <vector>
#define int long long
#define nmax (int)(1e5+5)
#define dim (int)(1e6+5)
using namespace std;
ifstream cin("pairs.in");
ofstream cout("pairs.out");
int n,sol,x,maxi,nr[dim];
bool mobius[dim];
vector<int>p;
signed main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;
        for(int d=1;d*d<=x;d++)
            if(x%d==0){
                nr[d]++;
                if(x/d!=d)
                    nr[x/d]++;
            }
        maxi=max(maxi,x);
    }
    for(int i=2;i<=maxi;i++){
        int t=0,x=i;
        for(int d=2;d*d<=x;d++)
            if(x%d==0){
                if(x/d%d==0){
                    x=0;
                    break;
                }
                x/=d;
                t++;
            }
        if(x==0)
            continue;
        if(x!=1)
            t++;
        if(t%2==0)
            mobius[i]=-1;
        else
            mobius[i]=1;
    }
    for(int i=2;i<=maxi;i++)
        if(nr[i]>1)
            sol+=nr[i]*(nr[i]-1)/2*mobius[i];
    cout<<n*(n-1)/2-sol;
    return 0;
}