Cod sursa(job #2479118)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 23 octombrie 2019 11:44:53
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <cstdio>

using namespace std;

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

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

        scanf("%d", &n);
        while (n--) {
                int x;
                scanf("%d", &x);
                cnt[x]++;
        }

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

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


        return 0;
}