Cod sursa(job #1334361)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 4 februarie 2015 12:02:59
Problema Pairs Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX_N = 100002;
const int MAX_VAL = 1000002;

int N, ans;
int v[MAX_N], cnt[MAX_VAL], a[15];
vector < int > primes;
bool isPrime[MAX_VAL];

int main() {
    ifstream f("pairs.in");
    ofstream g("pairs.out");

    f >> N;
    for(int i = 1; i <= N; ++i)
        f >> v[i];

    primes.push_back(2);
    for(int i = 3; i < MAX_VAL; i += 2)
        isPrime[i] = 1;
    for(int i = 3; i < MAX_VAL; i += 2)
        if(isPrime[i]) {
            primes.push_back(i);
            for(int j = i + i; j < MAX_VAL; j += i)
                isPrime[j] = 0;
        }

    sort(v + 1, v + N + 1);
    for(int i = 1; i <= N; ++i) {
        int x = v[i], n = 0;
        for(int j = 0; j < (int) primes.size() && v[j] * v[j] <= x; ++j)
            if(x % primes[j] == 0) {
                a[++n] = primes[j];
                while(x % primes[j] == 0)
                    x /= primes[j];
            }
        if(x > 1)
            a[++n] = x;

        int now = 0;
        for(int conf = 1; conf < (1 << n); ++conf) {
            int k = 0, p = 1;

            for(int j = 0; j < n; ++j) {
                if(conf & (1 << j)) {
                    ++k;
                    p *= a[j + 1];
                }
            }

            if(k % 2)
                now += cnt[p];
            else now -= cnt[p];
        }

        now = i - 1 - now;
        ans += now;

        int p = 1;
        for(int j = 1; j <= n; ++j)
            ++cnt[a[j]];
    }

    g << ans << "\n";

    f.close();
    g.close();

    return 0;
}