Cod sursa(job #329049)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 4 iulie 2009 15:49:32
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <stdio.h>
#include <values.h>
#define Nmax 100005
#define Mmax 1000005
#define LL long long

int cnt[Mmax];
bool p[Mmax],bad[Mmax],exp[Mmax];
int n;
LL res;

int main(){
	freopen("pairs.in","r",stdin);
   freopen("pairs.out","w",stdout);
   scanf("%d",&n);
   int i,j,x;
   for(i=1;i<=n;++i){
   	 scanf("%d",&x);
       cnt[x]++;
   }
   res=(LL)n*(n-1)/2;

   for(i=2; i<Mmax; ++i){
     for(j=2*i; j<Mmax; j+=i)
       cnt[i]+=cnt[j]; // nr care se divid cu j se divid si cu i
     if(!p[i]){
        exp[i]=1;
	     for(j=2*i; j<Mmax; j+=i){
            p[j]=1;
        		if((j/i)%i==0) bad[j]=1;
            exp[j] ^=1;
        }
     }

     if(!bad[i])
       if(!exp[i])
         res += (LL)cnt[i]*(cnt[i]-1)/2;
       else res -= (LL)cnt[i]*(cnt[i]-1)/2;
   }

   printf("%lld\n",res);
   fclose(stdin); fclose(stdout);
   return 0;
}