Cod sursa(job #120423)

Utilizator DastasIonescu Vlad Dastas Data 5 ianuarie 2008 14:18:27
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <cstdio>

const int maxn = 1000001;
const int maxp = 2000;

FILE *in = fopen("pairs.in","r"), *out = fopen("pairs.out","w");

int n;

int k;
char x[maxn]; // pt numere prime
int nrp[maxn]; // nrp[i] = din cate numere prime e alcatuit i. -1 daca i e invalid

int maxnr = -1;

char e[maxn];
int h[maxn];

void read()
{
    fscanf(in, "%d", &n);

    int x = 0;
    for ( int i = 1; i <= n; ++i )
        fscanf(in, "%d", &x), ++e[ x ], x > maxnr ? maxnr = x : 1;
}

void calc()
{
    for ( int i = 2; i <= maxnr; ++i )
    {
        if ( !x[i] ) // numara numarul de divizori
        {
            for ( int j = i; j <= maxnr; j += i )
            {
                ++nrp[j], x[j] = 1;

                if ( e[j] )
                    ++h[i];
            }
        }
        else
            for ( int j = i; j <= maxnr; j += i )
                if ( e[j] )
                    ++h[i];
    }

    // elimina multipli ai patratelor perfecte

    for ( int i = 2; i*i <= maxnr; ++i )
        for ( int j = i*i; j <= maxnr; j = j + i*i )
            nrp[j] = -1;
}

long long count()
{
    long long res = 0;

    for ( int i = 2; i <= maxnr; ++i )
        if ( nrp[i] != 0 && nrp[i] != -1 )
        {
            if ( (nrp[i] & 1) == 0 )
                res = res - (((long long)h[i]*(h[i]-1)) >> 1);
            else
                res = res + (((long long)h[i]*(h[i]-1)) >> 1);
        }

    return res;
}

int main()
{
    read();
    calc();

    long long cnt = (long long)n*(n-1)/2;

    fprintf(out, "%lld\n", cnt - count());

	return 0;
}