Pagini recente » Cod sursa (job #1190830) | Cod sursa (job #716148) | Cod sursa (job #1140765) | Cod sursa (job #2611330) | Cod sursa (job #996726)
Cod sursa(job #996726)
#include<iostream>
#include<fstream>
#include<math.h>
#include<bitset>
using namespace std;
#define NMAX 100001
#define VMAX 1000001
int v[NMAX],nr[VMAX],d[NMAX],p[NMAX];
bitset <VMAX> viz;
void ciur()
{
int i,j;
for(i=2;i<=VMAX-1;i++)
if(viz[i]==0) {
p[++p[0]]=i;
for(j=i+i;j<=VMAX-1;j=j+i)
viz[j]=1;
}
}
int main ()
{
int i,j,stop,k,l,count,n,x;
long long sol,prod;
ifstream f("pairs.in");
ofstream g("pairs.out");
f>>n;
for(i=1;i<=n;i++)
f>>v[i];
f.close();
sol=1LL*n*(n-1)/2;
ciur();
for(i=n;i>=1;i--) {
k=0;
x=v[i];
for(j=1;p[j]*p[j]<=x;j++)
if(v[i]%p[j]==0) {
d[++k]=p[j];
while(v[i]%p[j]==0)
v[i]=v[i]/p[j];
}
if(v[i]!=1)
d[++k]=v[i];
for(j=1;j<=(1<<k)-1;j++) {
prod=1;
count=0;
for(l=0;l<=k-1;l++)
if(j&(1<<l)) {
prod=1LL*prod*d[l+1];
count++;
}
if(count%2)
sol=0LL+sol-nr[prod];
else sol=0LL+sol+nr[prod];
nr[prod]++;
}
}
g<<sol;
g.close();
return 0;
}