Cod sursa(job #2093980)

Utilizator ialexia_ioanaAlexia Ichim ialexia_ioana Data 24 decembrie 2017 18:55:13
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <cstdio>
#include <algorithm>

using namespace std;
const int MAXN=1000000;
bool v[MAXN+5];
int prime[MAXN+5];


int main()
{
    freopen("pairs.in", "r", stdin);
    freopen("pairs.out", "w", stdout);
    int n, i, j, MAX=-1, x, nr=0;
    long long neprime=0;
    scanf("%d", &n);
    for(i=1; i<=n; i++)
    {
        scanf("%d", &x);
        v[x]=1;
        if (x>MAX)
            MAX=x;
    }
    for(i=2; i<=MAX; i++)
    {
        if (prime[i]==0)
        {
            for(j=i; j<=MAX; j+=i)
                prime[j]++;
        }
    }
    for(i=2; i*i<=MAX; i++)
    {
        for(j=i*i; j<=MAX; j+=i*i)
            prime[j]=-1;
    }
    for(i=2; i<=MAX; i++)
    {
        if (prime[i]==-1)
            continue;
        nr=0;
        for(j=i; j<=MAX; j+=i)
        {
            if (v[j]==1)
                nr++;
        }
        if (prime[i]%2==1)
            neprime+=(long long)nr*(nr-1)/2;
        else
            neprime-=(long long)nr*(nr-1)/2;
    }
    printf("%lld\n", (long long)n*(n-1)/2-neprime);
    return 0;
}