Cod sursa(job #3290994)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 2 aprilie 2025 20:17:21
Problema Pairs Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 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],mobius[dim];
bool ciur[dim];
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);
    }
    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+=nr[i]*(nr[i]-1)/2*mobius[i];
    cout<<n*(n-1)/2-sol;
    return 0;
}