Cod sursa(job #199507)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 19 iulie 2008 00:15:56
Problema Pairs Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <cstdio>
#define IN "pairs.in"
#define OUT "pairs.out"
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define N_MAX 1<<17
#define M_MAX 1<<20
#define SQRT 1<<11
#define LL long long

int max,N;
LL rez;
int nrpr[SQRT*2],V[N_MAX];
bool pr[SQRT*2],bo[M_MAX];


void ciur()
{
	nrpr[++ nrpr[0] ] = 2;
	FOR(i,3,SQRT)
	{
		if(pr[i])
			continue;
		nrpr[ ++nrpr[0] ] =i;
		FOR(j,i*i,SQRT)
			pr[j] = 1,
			j += i-1;
	}
}
	
void scan()
{
	freopen(IN, "r",stdin);
	freopen(OUT, "w",stdout);
	scanf("%d", &N);
	FOR(i,1,N)
	{
		scanf("%d", &V[i]);
		if(V[i] > max)
			max = V[i];
		bo[ V[i] ] = 1;
 	}	
}

void solve()
{
	FOR(i,2,max)
	{
		int nr=0,ok=1,aux=i,nrdiv=0;
		for(int j=1;j<= nrpr[0] && ok && nrpr[j] * nrpr[j] <= aux;++j)
			if(!(aux % nrpr[j]))
			{
				aux /= nrpr[j];
				++nrdiv;
				if(!(aux % nrpr[j]))
					ok=0;
			}
		
		if(!ok)	
			continue;	
		if(aux>1)
			++nrdiv;
		
		FOR(j,1,max/i)
			nr += bo[ i*j ];
		if(nrdiv & 1)
			rez += (LL) nr * (nr-1) / 2;
		else
			rez -= (LL) nr * (nr-1) / 2;
	}	
	
	printf("%lld\n", (LL) (N * (N-1) / 2) - rez); 
	
}

int main()
{
	scan();
	ciur();
	solve();
	return 0;
}