Cod sursa(job #3290997)

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