Cod sursa(job #2479123)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 23 octombrie 2019 11:54:27
Problema Pairs Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <cstdio>

using namespace std;

const int S = 1 << 17;
char buf[S];
int ptr = S;

char urm()
{
        if (ptr == S)
        {
                fread(buf, 1, S, stdin);
                ptr = 0;
        }
        return buf[ptr++];
}

int read()
{
        int ans = 0;
        char c = urm();
        while (c < '0' || '9' < c)
                c = urm();
        while ('0' <= c && c <= '9')
        {
                ans = 10 * ans + c - '0';
                c = urm();
        }
        return ans;
}

const int N = 1000000 + 7;
int n, cnt[N], mx;
long long sol[N];

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

        n = read();
        while (n--)
        {
                int x = read();
                cnt[x]++;
                if (x > mx)
                        mx = x;
        }

        for (int i = 1; i <= mx; i++)
                for (int j = 2 * i; j <= mx; j += i)
                        cnt[i] += cnt[j];

        for (int i = mx; i >= 1; i--)
        {
                sol[i] = cnt[i] * (long long) (cnt[i] - 1) / 2;
                for (int j = 2 * i; j < N; j += i)
                        sol[i] -= sol[j];
        }
        printf("%lld\n", sol[1]);



        return 0;
}