Cod sursa(job #1370603)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 3 martie 2015 16:02:39
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <bits/stdc++.h>

using namespace std;

///-------------------------
const int BS = (1 << 18);
char buffer[BS];
int position = BS;

inline char getChar()
{
    if ( position == BS )
    {
        position = 0;
        fread(buffer, BS, 1, stdin);
    }

    return buffer[position++];
}

inline int getNr()
{
    int a = 0;
    char ch;

    do
    {
        ch = getChar();

    } while ( !isdigit(ch) );

    do
    {
        a = (a << 3) + (a << 1) + ch - '0';
        ch = getChar();

    } while ( isdigit(ch) );

    return a;
}
///-------------------------

const int Vmax = 1000000 + 1;

int sieve[Vmax];
int use[Vmax];
bool prime[Vmax];
bool valid[Vmax];

int N, M;

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

    N = getNr();

    for ( int i = 1; i <= N; ++i )
    {
        int a = getNr();
        use[a]++;
        M = max(M, a);
    }

    long long sol = 0;

    for ( int i = 2; i <= M; ++i )
    {
        if ( prime[i] == false )
        {
            for ( int j = i; j <= M; j += i )
            {
                sieve[j]++;
                prime[j] = true;

                if ( j % (i * i) == 0 )
                    valid[j] = true;
            }
        }
    }

    for ( int i = 2; i <= M; ++i )
    {
        if ( valid[i] == false )
        {
            int nr = 0;

            for ( int j = i; j <= M; j += i )
                nr += use[j];

            if ( sieve[i] & 1 )
                sol += 1LL * nr * (nr - 1) / 2;
            else
                sol -= 1LL * nr * (nr - 1) / 2;
        }
    }

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

    return 0;
}