Cod sursa(job #1370581)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 3 martie 2015 15:54:33
Problema Pairs Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <bits/stdc++.h>

using namespace std;

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

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

    return buffer[position++];
}

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;
        a = getNr();
        use[a] = 1;
        M = max(M, a);
    }

    long long sol = 1LL * N * (N - 1) / 2;

    for ( int i = 2; i <= M; ++i )
    {
        for ( int j = i + i; j <= M; j += i )
            if ( use[j] )
                use[i]++;

        if ( prime[i] == false )
        {
            sieve[i] = 1;

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

                if ( valid[j] == false )
                {
                    if ( (j / i) % i == 0 )
                        valid[j] = true;

                    sieve[j]++;
                }
            }
        }

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

    printf("%lld\n", sol);

    return 0;
}