Cod sursa(job #1752280)

Utilizator Bodo171Bogdan Pop Bodo171 Data 3 septembrie 2016 13:51:37
Problema Pairs Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <iostream>
#include<fstream>
#include<bitset>
using namespace std;
const int nmax=1000000;
bool ap[nmax+5],put[nmax+5];
int x,n,i,j,grd[nmax+10],ad;
long long tot,nr,cpy;
int c;
int main()
{
    ifstream f("pairs.in");
    ofstream g("pairs.out");
    f>>n;
    for(i=1;i<=n;i++)
    {
        f>>x;
        ap[x]=1;
    }
    nr=n;
    tot=(nr*(nr-1))/2;
    for(i=2;i<=nmax;i++)
    {
     if(grd[i]==0)
        for(j=i;j<=nmax;j+=i)
      {
        if((j/i)%i==0) put[j]=1;
        grd[j]++;
      }
      if(grd[i]%2==0) ad=-1;
      else ad=1;
      c=0;
    if(!put[i])
      for(j=i;j<=nmax;j+=i)
      {
          if(ap[j]) c++;
      }
      cpy=c;
      if(ad==-1)tot=tot+(cpy*(cpy-1))/2;
      else tot=tot-(cpy*(cpy-1))/2;
    }
    g<<tot;
    return 0;
}