Cod sursa(job #3220276)

Utilizator Radu_MocanasuMocanasu Radu Radu_Mocanasu Data 2 aprilie 2024 23:54:24
Problema Pairs Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("pairs.in");
ofstream fout("pairs.out");
bitset <1000005> c;
vector <int> prime;
long long rez;
int f[1000002];
int fp[12];
void ciur(int n){
    c[0] = c[1] = 1;
    for(int i = 4; i <= n; i += 2) c[i] = 1;
    for(int i = 3; i * i <= n; i++){
        if(!c[i]){
            for(int j = i * i; j <= n; j += 2 * i) c[j] = 1;
        }
    }
    for(int i = 1; i <= n; i++) if(!c[i]) prime.push_back(i);
}
void solve(int n, int t){
    for(int e = 1; e < (1 << t); e++){
        int p = 1,nrb = 0;
        for(int i = 0; i < t; i++){
            if((1 << i) & e){
                p *= fp[i];
                nrb++;
            }
        }
        if(nrb & 1) rez -= f[p];
        else rez += f[p];
    }
}

int main()
{
    int n,i,x;
    fin >> n;
    ciur(1e6);
    rez = n * (n - 1) / 2;
    for(i = 1; i <= n; i++){
        fin >> x;
        int e = x,t = 0;
        for(auto p : prime){
            if(e <= 1) break;
            if(p * p > e) p = e;
            if(e % p == 0){
                fp[t++] = p;
                while(e % p == 0) e /= p;
            }
        }
        solve(x,t);
        int d = 1;
        for(d = 1; d * d < x; d++){
            if(x % d == 0){
                f[d]++;
                f[x / d]++;
            }
        }
        if(d * d == x) f[d]++;
    }
    fout << rez;
    return 0;
}