Cod sursa(job #1752347)

Utilizator GoogalAbabei Daniel Googal Data 3 septembrie 2016 16:18:00
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <algorithm>
#include <iostream>
#define nmax 100111
#define lmax 1000111

using namespace std;

ifstream fin("pairs.in");
ofstream fout("pairs.out");

int n,i,j,x,maxx,a[lmax],nr[lmax];
long long cmbin[nmax][3],s;

void read()
{
fin>>n;
for (i=1;i<=n;i++)
{
  fin>>x;
  a[x]=1;
  maxx=max(maxx,x);
}
}

void clearr()
{
   for (i=2;i*i<=maxx;i++)
    for (j=i*i;j<=maxx;j+=i*i)
     nr[j]=0;
}

void solve()
{
  for (i=1;i<=n;i++)
{
    cmbin[i][0]=1;
    for (j=1;j<=2;j++)
        cmbin[i][j]=cmbin[i-1][j-1]+cmbin[i-1][j];
}
for (i=2;i<=maxx;i++)
    if (nr[i]==0)
    {
        for (j=i;j<=maxx;j+=i)
         nr[j]++;
    }

    clearr();

for (i=2;i<=maxx;i++)
    if (nr[i]>0) {
       int l=0;
       for (j=i;j<=maxx;j+=i)
            l+=a[j];
       if (nr[i]%2==1)
          s=s+cmbin[l][2];
       else
        s=s-cmbin[l][2];
    }
}

int main()
{
read();
cmbin[0][0]=1;
solve();
s=(1LL*n*(n-1))/2-s;
fout<<s;
return 0;
}