Cod sursa(job #995849)

Utilizator cont_de_testeCont Teste cont_de_teste Data 10 septembrie 2013 12:57:06
Problema Pairs Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <cstdio>
using namespace std;


const int MAX_N = 100005;
const int MAX_V = 1000006;

int n;
int apr[MAX_V];

int a[MAX_N];
bool prime[MAX_V];
int good[MAX_V];

int dvs[MAX_V];
long long soln;



const int bs = 1 << 20;
char buff[bs];
inline bool digit(const char &a) {
    if ('0' <= a && a <= '9') return true;
    return false;
}
inline void next(int &n) {
    n = 0;
    static int i = bs;
      
    if (i == bs) {
        fread(buff, 1, bs, stdin), i = 0;
    }
    while (!digit(buff[i])) {
        if (++i == bs) {
            fread(buff, 1, bs, stdin), i = 0;
        }
    }
    while (digit(buff[i])) {
        n = n * 10 + buff[i] - '0';
        if (++i == bs) {
            fread(buff, 1, bs, stdin), i = 0;
        }
    }
}



void read() {
	next(n);
	for (int i = 1; i <= n; ++i) {
		next(a[i]);
		++apr[a[i]];
	}
}


void make_pr() {
	for (int i = 2; i < MAX_V; ++i) {
		if (!prime[i]) {
			good[i] = 1;
			for (int j = i + i; j < MAX_V; j += i) {
				prime[j] = true;
				if ( (j / i) % i ) {
					++good[j];
				} else {
					good[j] = -MAX_V;
				}
			}
		}
	}
	for (int i = 2; i < MAX_V; ++i) {
		for (int j = i; j < MAX_V; j += i) {
			dvs[i] += apr[j];
		}
	}
}

const int max_int = 1000000000;
inline void prt_ll(const long long &n) {
    if (n / max_int) {
        printf("%i", (int)(n / max_int));
        printf("%.9i", (int)(n % max_int));
    } else {
        printf("%i", n % max_int);
    }
}


void solve() {
	soln = 1LL * n * (n - 1) / 2LL;
	for (int i = 2; i < MAX_V; ++i) {
		if (good[i] > 0) {
			if (good[i] % 2) {
				soln -= 1LL * dvs[i] * (dvs[i] - 1) / 2LL;
			} else {
				soln += 1LL * dvs[i] * (dvs[i] - 1) / 2LL;
			}
		}
	}
}


void write() {
	prt_ll(soln);
}


int main() {
	freopen("pairs.in", "r", stdin);
	freopen("pairs.out", "w", stdout);
	read();
	make_pr();
	solve();
	write();
}