Pagini recente » Cod sursa (job #1153863) | Cod sursa (job #2260520) | Cod sursa (job #576790) | Cod sursa (job #1291720) | Cod sursa (job #182844)
Cod sursa(job #182844)
#include<stdio.h>
int a[100010],ok2,nrf,aux,ok,d,m,res,p,n,i,v[100010],max,j;
char ap[1000010],t[1100110];
int P[100011];
int main(){
FILE *f=fopen("pairs.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++){
fscanf(f,"%d",&v[i]);
if(v[i]>max)
max=v[i];
ap[v[i]]=1;
}
fclose(f);
for(i=2;i<=max;i++){
for(j=i;j<=max;j+=i){
if(ap[j])
a[i]++;
}
}
for(i=2;i<=max;i++){
if(!t[i])
for(j=i+i;j<=max;j+=i)
t[j]=1;
}
p=0;
for(i=2;i<=max;i++)
if(!t[i]){
p++;
P[p]=i;
}
for(i=2;i<=max;i++){
if(a[i]){
nrf=0;
aux=i;
ok=1;
d=1;
m=0;
while(aux!=1&&d<=p){
ok2=1;
m=0;
while(!(aux%P[d])){
if(ok2)
nrf++;
m++;
aux/=P[d];
}
if(m>1){
ok=0;
break;
}
d++;
}
if(ok){
if(aux>1)
nrf++;
if(nrf%2==1)
res+=(a[i]*(a[i]-1))/2;
else
res-=(a[i]*(a[i]-1))/2;
}
}
}
FILE *g=fopen("pairs.out","w");
fprintf(g,"%d",n*(n-1)/2 - res);
fclose(g);
return 0;
}