Cod sursa(job #1515180)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 1 noiembrie 2015 11:30:11
Problema Pairs Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>

#define lint long long int
using namespace std;

const int NMAX = 100005;
const int VALMAX = 1000000;

int factor[VALMAX + 5];
int mobius[VALMAX + 5];

inline void erat () {
    int j;
    for (int i = 2; i <= 1000000; ++ i)
        if (!factor[i])
            for (j = i; j / i <= i && j <= 1000000; j += i)
                factor[j] = i;

    mobius[1] = 1;
    for (int i = 2; i <= VALMAX; ++ i) {
        if (!factor[i]) {
            factor[i] = i;
            mobius[i] = -1;
        }

        if (factor[i] != factor[i / factor[i]])
            mobius[i] = (-1) * mobius[i / factor[i]];
    }
}

int frecv[VALMAX + 5];

int main()
{
    ifstream cin("pairs.in");
    ofstream cout("pairs.out");

    erat();

    int n = 0;
    cin >> n;

    int val;
    for (int i = 1; i <= n; ++ i) {
        cin >> val;
        ++ frecv[val];
    }

    lint ans = 0;
    for (int i = 1, j; i <= VALMAX; ++ i)
        if (mobius[i]) {
            for (j = 2 * i; j <= VALMAX; j += i)
                frecv[i] += frecv[j];
            ans += mobius[i] * (frecv[i] * (frecv[i] - 1ll)) / 2;
        }

    cout << ans << '\n';

    cin.close();
    cout.close();
    return 0;
}