Cod sursa(job #3290998)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 2 aprilie 2025 20:32:38
Problema Pairs Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <vector>
#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;
long long nr[dim],sol;
bool ciur[dim];
vector<int>p;
void bck(int last,int t,long long val){
    if(t!=0){
        if(t%2!=0){
            if(nr[val]>1)
                sol+=nr[val]*(nr[val]-1)/2;
        }else if(nr[val]>1)
            sol-=nr[val]*(nr[val]-1)/2;
    }
    for(int i=last;i<p.size()&&1LL*val*p[i]<=maxi;i++)
        bck(i+1,t+1,val*p[i]);
}
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);
    }
    for(int i=2;i<=maxi;i++)
        if(!ciur[i]){
            p.push_back(i);
            for(int j=i*2;j<=maxi;j+=i)
                ciur[j]=1;
        }
    bck(0,0,1);
    cout<<1LL*n*(n-1)/2-sol;
    return 0;
}