Cod sursa(job #1752192)

Utilizator silkMarin Dragos silk Data 2 septembrie 2016 23:00:11
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <cstdio>
#define NMax 1000000
#define ll long long

int div[NMax+1];
int v[NMax+1];

void Precalc()
{
    int i,j;

    for(i = 2; i <= NMax; i+=2) div[i] += 1;

    for(i = 3; i <= NMax; i+=2)
    if( !div[i] )
        for(j = i; j <= NMax; j+=i) div[j] += 1;
}

int main(){
    freopen("pairs.in","r",stdin);
    freopen("pairs.out","w",stdout);

    int i,j,N,x;
    ll temp,ans;

    Precalc();

    scanf("%d",&N);
    for(i = 1; i <= N; ++i)
    {
        scanf("%d",&x);
        v[x] = 1;
    }

    ans = 0;
    for(i = 2; i <= NMax; ++i)
    if( div[i] )
    {
        temp = 0;
        for(j = i; j <= NMax; j+=i)
        if( v[j] ) ++temp;

        if( div[i]%2 && temp ) ans = ans + temp*(temp-1)/2;
        else if( temp ) ans = ans - temp*(temp-1)/2;

        if( i <= 1000 )
            for(j = i*i; j <= NMax; j+=i*i) div[j] = 0;
    }


    printf("%lld\n", 1LL*N*(N-1)/2 - ans );


return 0;
}